简体   繁体   English

如何在 javascript 中检查和添加属性到数组 object

[英]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.我想知道如何在 javascript 的数组 object 中添加两个键。

In object obj in place of title, add label and value in javascript.在 object obj中代替标题,添加label and value

Each children key may have children key, if it has children and title key, add label and value key in javascript每个children key可能有children key,如果有children和title key,在javascript中添加label和value key

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在现代 JS 语法中,它可以这样实现

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 [...] .在这种情况下, obj是由[...]表示的对象数组。 Arrays can be iterated over using built in functions. Arrays 可以使用内置函数进行迭代。 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 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.您应该解决将 label 和值添加到顶级 object 的问题,然后从顶级 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.习惯阅读文档,它们将是您解决问题时的第一个来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中将数组中的元素添加为对象的属性 - How to add elements in an array as properties of an object in JavaScript 如何在 javascript 中检查对象的属性数组是否为空 - How to check if array of object's properties are empty in javascript 如何在JavaScript中将对象属性添加到数组中? - How do I add object properties into an array in JavaScript? 如何动态地向javascript数组中的每个对象添加属性 - How to add properties dynamically to each object in an javascript array 如何在 javascript 中将数组转换为 object 时添加其他属性? - How to add additional properties while converting array to object in javascript? 如何将 object 作为属性添加到数组 - How to add object as properties to array 如何在 JavaScript 的条件语句中检查对象属性? - How to check for object properties in a conditional statement in JavaScript? 如何检查object是否在JavaScript中有任何属性? - How to check if object has any properties in JavaScript? Javascript,如何将 object 添加到数组中,如果数组中不存在此 object 的某些属性 - Javascript, how to add an object to an array, if some properties of this object dont already exist in the array JavaScript:如何将数组中的值添加到来自不同数组的 object 属性? - JavaScript: How to add to values from an array to object properties which are from different array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM