1

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.

2
  • 2
    Is the object will only have 2 properties ( off and off1 only ) or are we expecting more? Commented Mar 21, 2018 at 5:42
  • Thanks all for your time and help at right time .. keep helping others !!! Commented Mar 21, 2018 at 7:31

6 Answers 6

4

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

3

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

Comments

3

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

Comments

3

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.