Elamparithi.P
Elamparithi.P

Reputation: 149

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.

Upvotes: 1

Views: 9011

Answers (6)

Rahul Sharma
Rahul Sharma

Reputation: 10071

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);

Upvotes: 4

Alexis
Alexis

Reputation: 842

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);

Upvotes: 1

Dyapa Srikanth
Dyapa Srikanth

Reputation: 1241

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);

Upvotes: 4

H77
H77

Reputation: 5967

This solution handles the case where you might have more than off and off1 (e.g. 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);

Upvotes: 3

Mamun
Mamun

Reputation: 68933

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);

Upvotes: 3

wscourge
wscourge

Reputation: 11291

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);

Upvotes: 3

Related Questions