简体   繁体   中英

How to find a value in nested array in javascript

This is my query ,

var id = [1,2];

var oparr = [{
    "off": [{
        id: 1,
        val: "aaa"
      },
      {
        id: 3,
        val: "bbb"
      }
    ]
  },
  {
    "off1": [{
      id: 2,
      val: "cccc"
    }]
  }
];

from the above oparr array I need to find with id array, I need this result

var arr = {
    finalval: [
    {
        id: 1,
        off: true,
        off1: false
    }, 
    {
        id: 2,
        off: false,
        off1: true
    }]
}

If the id is in off[0] I need set off1 as true and off2 as false.

I tried with underscore js indexof , find , findWhere but I didn't get the desired result format.

Try this

 var id = [1, 2]; var oparr = [{ "off": [{ id: 1, val: "aaa" }, { id: 3, val: "bbb" } ] }, { "off1": [{ id: 2, val: "cccc" }] } ]; var b = id.map((x) => { return oparr.reduce((a, data) => { var key = Object.keys(data)[0]; Object.assign(a, data[key].reduce((obj, ele) => { if (!obj[key]) obj[key] = (ele.id == x); return obj; }, { id: x })); return a; }, {}) }) console.log(b); 

 var oparr = [ { "off":[ { id:1, val:"aaa" }, { id:3, val:"bbb" } ] }, { "off1":[ { id:2, val:"cccc" } ] } ]; var offs = []; var finalval = []; for(var i=0; i<oparr.length; i++) { var _tmp1 = oparr[i]; for(var prop in _tmp1) { offs.push(prop); var _tmp2 = _tmp1[prop]; for(var j=0; j<_tmp2.length; j++) { var result = {}; finalval.push(result); result.id = _tmp2[j].id; result[prop] = true; } } } for(var i=0; i<finalval.length; i++) { for(var j=0; j<offs.length; j++) { if (!finalval[i][offs[j]]) { finalval[i][offs[j]] = false; } } } var arr = {"finalval":finalval}; console.log(arr); 

 var id = [1,2]; var oparr = [ { "off": [ { id: 1, val: "aaa" }, { id: 3, val: "bbb" } ] }, { "off1": [ { id: 2, val: "cccc" } ] } ]; var arr = { finalval: [ { id: 1, off: true, off1: false }, { id: 2, off: false, off1: true } ] } var arr = {}; arr.finalval = id.map((i) => { return { id : i, off : oparr.find(object => "off" in object)["off"].some(object => object.id == i), off1: oparr.find(object => "off1" in object)["off1"].some(object => object.id == i), } }); console.log(arr.finalval); 

You can try the following with Array's forEach() :

 var id = [1,2]; var oparr = [ { "off": [ { id: 1, val: "aaa" }, { id: 3, val: "bbb" } ] }, { "off1": [ { id: 2, val: "cccc" } ] }]; var arr = {finalval: []}; oparr.forEach(function(item){ for(var key in item){ item[key].forEach(function(i){ if(id.includes(i.id) && key == 'off') arr.finalval.push({id: i.id, off: true, off1: false}); else if(id.includes(i.id) && key == 'off1') arr.finalval.push({id: i.id, off: false, off1: true}); }); } }); console.log(arr); 

This solution handles the case where you might have more than off and off1 (eg off, off1, off2, off3 etc.).

 var id = [1, 2]; var oparr = [{ "off": [{ id: 1, val: "aaa" }, { id: 3, val: "bbb" } ] }, { "off1": [{ id: 2, val: "cccc" }] } ]; // get the off options (off, off1, off2 etc.) var offOptions = oparr.map(op => Object.keys(op)).reduce((a, c) => a.concat(c), []); var arr = { finalval: id.map(x => { var result = { id: x }; // iterate through off options offOptions.forEach(op => { // iterate through oparr oparr.forEach(o => { var vals = o[op]; if (vals) // check if off option exists result[op] = vals.some(i => i.id === x); // check if index exists }); }); return result; }) }; console.log(arr); 

I hope this will help. Thank you.

 var id = [1, 2]; var oparr = [{ "off": [{ id: 1, val: "aaa" }, { id: 3, val: "bbb" } ] }, { "off1": [{ id: 2, val: "cccc" }] } ]; var test=[]; for (var i = 0; i < oparr.length; i++) { for (var j = 0; j < Object.keys(oparr[i]).length; j++) { for (var k = 0; k < id.length; k++) { if(oparr[i][Object.keys(oparr[i])[j]][j].id==id[k]){ test.push(oparr[i][Object.keys(oparr[i])[j]][j]); } } } } console.log(test); 

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