3

I have the following array that I need to loop through:

var arr = [{
  "id": 111,
  "wbs_name": "Mechanical",
  "parent": 'root',
}, {
  "id": 222,
  "wbs_name": "Electrical",
  "parent": 111,
}, {
  "id": 333,
  "wbs_name": "Systems",
  "parent": 111,
},]

My output should be like this:

 var mechanical = {
     "id": 111,
     "wbs_name": "mechanical",
     "parent": 0,
 },

 var electrical= {
     "id": 222,
     "wbs_name": "electrical",
     "parent": mechanical,
 },

 var systems = {
     "id": 222,
     "wbs_name": "systems",
     "parent": mechanical,
 },

I already tried looping through the array and pushing the object into another, but I don't know how to assign them to a variable at the same time (where the variable name is "wbs_name" and the "parent" is the variable name of some other parent object.

3
  • 3
    arr.forEach( s => { window[s.wbs_name] = s; } ) Commented Feb 26, 2018 at 9:22
  • @gurvinder372 This is the code I'm looking for. Thanks! Commented Feb 26, 2018 at 10:06
  • I have added the same as an answer as well. Commented Feb 26, 2018 at 10:08

5 Answers 5

2

Try this,

var mechanical = filterList('Mechanical');
var electrical = filterList('Electrical');
var systems = filterList('Systems');
function filterList(filterBy){
    return arr.filter((ad)=>{
        return ad.wbs_name == filterBy;
    })[0];
};
console.log(mechnical);
console.log(electrical);
console.log(systems);

will get o/p as -

"mechnical"= {
  id: 111, 
  wbs_name: "Mechanical", 
  parent: "root"
}
Sign up to request clarification or add additional context in comments.

Comments

0

Though polluting the global scope isn't recommended, you can use the following

arr.forEach( s => { window[s.wbs_name] = s; } )

This code will create a global level variable for each id property of every object in the array.

Comments

0

var in a browser is part of the window object. We can assign it dynamically like this:

window['my-arbitrary-string'] = arbitrary_value

Comments

0

You can't declare different variables on the fly, but instead you can have an object and assign to it's properties. Then you can get the properties individually using object destructing

const root = {};

const arr = [{
   "id": 111,
   "wbs_name": "Mechanical",
   "parent": 'root',
}, {
   "id": 222,
   "wbs_name": "Electrical",
   "parent": 111,
}, {
   "id": 333,
   "wbs_name": "Systems",
   "parent": 111,
}];

arr.forEach(item => root[item.wbs_name.toLowerCase()] = Object.assign({}, item));

const { mechanical, electrical, systems } = root;
console.log(mechanical);
console.log(electrical);
console.log(systems);

Comments

0

You can use Object.assign and array.prototype.map

var arr = [
    {"id": 111, "wbs_name": "Mechanical", "parent": 'root'},
    {"id": 222, "wbs_name": "Electrical", "parent": 111},
    {"id": 333, "wbs_name": "Systems", "parent": 111}
];

var { mechanical, electrical, systems} = Object.assign({}, ...arr.map(e => { 
  var parent = arr.find(i => i.id === e.parent);  
  return { [e.wbs_name.toLowerCase()] : {...e, parent: parent ? parent.wbs_name.toLowerCase() : 0 } };
 }));
 
 console.log("mechanical: ", mechanical);
 console.log("electrical: ", electrical);
 console.log("systems: ", systems);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.