简体   繁体   中英

how to check and add properties to array object in javascript

I would like to know how to add two keys inside the array object in javascript.

In object obj in place of title, add label and value in javascript.

Each children key may have children key, if it has children and title key, add label and value key in javascript

var obj = [
  {
    id: 1,
    title: "Category"
    children: [
    {
    id:1, 
    title: "countries", 
    children: [
        {title: "IN", id:1},
        {title: "TH", id:2},
        {title: "SG", id:3}
      ]
    }, {
    id:2, 
    title: "fields", 
    children: [
        {title: "Finance", id:1},
        {title: "Services", id:2}
      ]
    }
    ]
  }
]

function changeObj(obj){
    if (obj.length > 0) {
    var result = obj.map(e => {
      if('children' in e)
        e.children = e.children.map(child => {
          if ('children' in child) 
            child.children = child.children.map(c =>({
             label: c.title,
             value: c.title
              })
            );
            return child;
        });      
      return e
    });
    return result;
  }
}
Expected Output
 {
    label: "Category",
    value: "Category",
    children: [
    {
    label: "countries", 
    value: "countries",
    children: [
        {label:"IN",value:"IN", title: "IN"},
        {label:"TH",value:"TH", title: "TH"},
        {label:"SG",value:"SG", title: "SG"}
      ]
    },{
    label: "fields", 
    value: "fields",
    children: [
        {label: "Finance", value: "Finance",title: "Finance"},
        {label: "Services", value: "Services",title: "Services"}
      ]
    }
    ]
  }


try it.

 function changeObj(obj) { if(.obj;length) return. obj.forEach(i => { const title = i;title. const child = i;children. if(i.id) { delete i;id. } if(i.title) { delete i;title. } i;label = title. i;value = title; if(child) { changeObj(child). } else { i;title = title; } }); return obj; }

In modern JS syntax it could be implemented this way

let __f = (_) => {
    let {title, children} = _;
    const value = title;
    const label = title;
    let result;
    if (children && children.length) {
        result = {label, value, children: children.map(__f)};
    } else {
        result = {label, value, title};
    }
    return result;
};

let result = obj.map(__f).pop();

in this instance obj is an array of objects as denoted by [...] . Arrays can be iterated over using built in functions. In your case, you wish to take each item in the array and alter it in some fashion. You will have the same number of elemnents at the end as you had at the beginning.

The map function passes each element in the source array to a function, populating a new array with the results of that function. The docs provide examples.

You should solve for adding the label and value to the top-level object, then delete the title from the top-level object. When you have that working, apply the same approach to the children.

[{ a: 1 }, { a: 2 }].map(item => ({ ...item, b: item.a + 1 }));
// [{ a: 1, b: 2 }, { a: 2, b: 3 }]

Get used to reading the docs, they will be your first source when solving a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM