简体   繁体   English

如何使用 javascript 中的数组值更改数组 object 中的属性值

[英]How to change property value in array object with array values in javascript

How to change property in the object array based on array of values in javascript,如何根据 javascript 中的值数组更改 object 数组中的属性,

I have object obj and parameter as list , should changed the property checked to true, if title matched with the list in javascript我有 object obj和参数作为list ,如果标题与 javascript 中的列表匹配,则应将属性checked更改为 true

if the title is matched with list and has children, then checked: true ie.如果标题与列表匹配并且有子项,则检查:true 即。 countries title is matched with list, then children also change the property checked: true

if the children title is matched, then change checked: true for that object only ie.如果子标题匹配,则更改已检查:仅适用于该 object 即。 clothes title is matched with list, then change the property checked: true for that object only clothes title is matched with list, then change the property checked: true于该 object

I stuck to do with list of arrays as param,我坚持使用 arrays 列表作为参数,

 var obj = [{ title: "Sample", checked: false, children: [{ title: "products", checked: false, children: [{ title: "clothes", id: 1, checked: false }, { title: "electronics", id: 2, checked: false } ] }, { title: "countries", checked: false, children: [{ title: "IN", id: 1, checked: false }, { title: "TH", id: 2, checked: false } ] } ] }]; var list = ["clothes", "countries"];

Expected Output:预期 Output:

[{
    title: "Sample",
    checked: false,
    children: [
    {
    title: "products", 
    checked: false,
    children: [
        {title: "clothes", id: 1, checked: true},
        {title: "electronics", id: 2, checked: false}
      ]
    },{
    title: "countries", 
    checked: true,
    children: [
        {title: "IN", id: 1, checked: true},
        {title: "TH", id: 2, checked: true}
      ]
    }
    ]
  }]

You need some recursion, to handle any depth of nesting:您需要一些递归来处理任何深度的嵌套:

With this solution, it is very generic, you can change the childrenProp, matchProp, checkedProp etc. and it will allow you to search as deep as you need to in a nested structure.使用此解决方案,它非常通用,您可以更改 childrenProp、matchProp、checkedProp 等,它可以让您在嵌套结构中搜索尽可能多的深度。

 const obj = [ { title: "Sample", checked: false, children: [ { title: "products", checked: false, children: [ {title: "clothes", id: 1, checked: false}, {title: "electronics", id: 2, checked: false} ] }, { title: "countries", checked: false, children: [ {title: "IN", id: 1, checked: false}, {title: "TH", id: 2, checked: false} ] } ] } ]; var list=["clothes", "countries"]; checkPropertyForMatch(obj, list); console.log(obj); checkPropertyForMatch(obj, ['electronics']); console.log(obj); function checkPropertyForMatch(inputArr, matchList, matchProp, childrenProp, checkedProp){ //default params, or they can be overwritten: matchProp = matchProp || 'title'; childrenProp = childrenProp || 'children'; checkedProp = checkedProp || 'checked'; innerRecursive(inputArr, matchList); //recursively search the nested object: function innerRecursive(currArr, list){ for (item of currArr){ if ( list.includes(item[matchProp]) ){ item[checkedProp] = true; if (item[childrenProp]){ //this parent matched, so mark all children as marked too: markAllChildrenChecked(item[childrenProp]); } } else { item[checkedProp] = false; if (item[childrenProp]) { //it didn't match but it has children, so search them too: innerRecursive(item[childrenProp], list) } } } } //this recursively marks all children as checked = true: function markAllChildrenChecked(currArr){ for (item of currArr){ item[checkedProp] = true; if (item[childrenProp]){ markAllChildrenChecked(item[childrenProp]); } } } }
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Output: Output:

[
  {
    "title": "Sample",
    "checked": false,
    "children": [
      {
        "title": "products",
        "checked": false,
        "children": [
          {"title": "clothes", "id": 1, "checked": true},
          {"title": "electronics", "id": 2, "checked": false}
        ]
      },
      {
        "title": "countries",
        "checked": true,
        "children": [
          {"title": "IN", "id": 1, "checked": true},
          {"title": "TH", "id": 2, "checked": true}
        ]
      }
    ]
  }
]

The trick here is below:这里的诀窍如下:

if (list.indexOf(ele.title) > -1 || list.indexOf(el.title) > -1) {
    el.checked = true;
}

 var obj = [{ title: "Sample", checked: false, children: [{ title: "products", checked: false, children: [{ title: "clothes", id: 1, checked: false }, { title: "electronics", id: 2, checked: false } ] }, { title: "countries", checked: false, children: [{ title: "IN", id: 1, checked: false }, { title: "TH", id: 2, checked: false } ] } ] }]; var list = ["clothes", "countries"]; obj.forEach(elem => { elem.children.forEach(ele => { ele.children.forEach(el => { if (list.indexOf(ele.title) > -1 || list.indexOf(el.title) > -1) { el.checked = true; } }) }) }) console.log(obj)

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

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