-
-
Save agmt5989/4b648da9727c2b6916be151c7c97fdbc to your computer and use it in GitHub Desktop.
| let a = [ | |
| { | |
| id: '1000000' | |
| }, | |
| { | |
| id: '1100000' | |
| }, | |
| { | |
| id: '1200000', | |
| }, | |
| { | |
| id: '11100000' | |
| }, | |
| { | |
| id: '11200000' | |
| }, | |
| { | |
| id: '11300000' | |
| }, | |
| { | |
| id: '12100000' | |
| }, | |
| { | |
| id: '121100000' | |
| }, | |
| ] | |
| let currentLevel = 1; | |
| let result_obj = {} | |
| function wtf(arrayOfObj) { | |
| let arr = arrayOfObj.filter(obj => { | |
| let {id} = obj; | |
| let firstZeroIndex = id.indexOf('0'); | |
| let sub = id.substring(0, firstZeroIndex); | |
| return sub.length === currentLevel | |
| }); | |
| if (arr.length <= 0) return result_obj; | |
| for (let i = 0; i < arr.length; i++) { | |
| let obj = arr[i]; | |
| let {id} = obj | |
| let firstZeroIndex = id.indexOf('0'); | |
| let sub = id.substring(0, firstZeroIndex); | |
| let split_sub = sub.split(''); | |
| if (split_sub.length === 1) { | |
| result_obj = { | |
| ...obj, | |
| children: {} | |
| }; | |
| break | |
| } | |
| split_sub.shift(); | |
| let part_obj = { | |
| ...obj, | |
| children: {} | |
| }; | |
| let attr_string = ''; | |
| for (let key of split_sub) { | |
| attr_string = `${attr_string}.children[${key}]` | |
| } | |
| let exec_string = `result_obj${attr_string} = part_obj` | |
| eval(exec_string); | |
| } | |
| currentLevel++; | |
| return wtf(arrayOfObj) | |
| } | |
| let b = wtf(a) | |
| console.log({b}) |
let Entity = require('./Entity')
let a = [
{
name: '112 BN',
sidc: '10031000161211000000',
position: {
latitude: 9.8717,
longitude: 8.1837
},
id: '100000000000000'
},
{
name: 'A COY',
sidc: '10031000151211000000',
position: {
latitude: 9.0421,
longitude: 7.6354
},
id: '110000000000000'
},
{
name: 'B COY',
sidc: '10031000151211000000',
position: {
latitude: 9.7846,
longitude: 6.4256
},
id: '120000000000000'
},
{
name: 'C COY',
sidc: '10031000151211000000',
position: {
latitude: 10.0421,
longitude: 7.9292
},
id: '130000000000000'
},
{
name: '1 PL',
sidc: '10031000141211000000',
position: {
latitude: 9.2387,
longitude: 6.0192
},
id: '111000000000000'
},
{
name: '2 PL',
sidc: '10031000141211000000',
position: {
latitude: 9.3984,
longitude: 6.8926
},
id: '112000000000000'
},
{
name: '3 PL',
sidc: '10031000141211000000',
position: {
latitude: 10.0231,
longitude: 7.0192
},
id: '113000000000000'
},
{
name: '4 PL',
sidc: '10031000141211000000',
position: {
latitude: 9.4562,
longitude: 7.0349
},
id: '121000000000000'
},
{
name: '6 PL',
sidc: '10031000141211000000',
position: {
latitude: 10.4223,
longitude: 5.8992
},
id: '122000000000000'
},
{
name: '7 PL',
sidc: '10031000141211000000',
position: {
latitude: 9.0891,
longitude: 5.3495
},
id: '131000000000000'
},
{
name: '8 PL',
sidc: '10031000141211000000',
position: {
latitude: 8.7421,
longitude: 7.0192
},
id: '132000000000000'
},
{
name: '9 PL',
sidc: '10031000141211000000',
position: {
latitude: 9.8764,
longitude: 7.6573
},
id: '133000000000000'
}
]
let currentLevel = 1;
let result_obj = {}
function wtf(arrayOfObj) {
let arr = arrayOfObj.filter(obj => {
let {id} = obj;
let firstZeroIndex = id.indexOf('0');
let sub = id.substring(0, firstZeroIndex);
return sub.length === currentLevel
});
if(arr.length <= 0) return result_obj;
for(let i = 0; i < arr.length; i++){
let obj = arr[i];
let {id, name, sidc, position} = obj
let firstZeroIndex = id.indexOf('0');
let sub = id.substring(0, firstZeroIndex);
let split_sub = sub.split('');
if(split_sub.length === 1){
result_obj = new Entity(name, sidc, position, id)
break
}
split_sub.shift();
let part_obj = new Entity(name, sidc, position, id)
let attr_string = '';
for(key of split_sub){
attr_string = ${attr_string}.children[${key}]
}
let exec_string = result_obj${attr_string} = part_obj
eval(exec_string);
}
currentLevel++;
return wtf(arrayOfObj)
}
let b = wtf(a)
console.log({b})
The next challenge is to use the fully-fledged objects, shown below:
to dynamically create instantiation objects for the
Entityclass, whose constructor is shown below: