christ
christ

Reputation: 69

JS map two object/array

I have 2 objects with format:

obj1 = [
          {
            "code": "in_today",
            "text": "Today"
          },
          {
            "code": "in_week",
            "text": "This week"
          },
          {
            "code": "in_month",
            "text": "This month"
          },
          {
            "code": "normal",
            "text": "Other"
          }
        ]

obj2 stores "code" value which defined in obj1:

obj2 = ["in_today", "in_week", "normal"]

How can I use obj2's values to change obj1 into something likes:

[
    {
        "code": "in_today",
        "text": "Today",
        "selected": true
    },
    {
        "code": "in_week",
        "text": "This week",
        "selected": true
    },
    {
        "code": "in_month",
        "text": "This month",
        "selected": false
    },
    {
        "code": "normal",
        "text": "Other"
        "selected": true
    }
]

What's the best solution for this case? Thanks!

Upvotes: 1

Views: 114

Answers (4)

dev8080
dev8080

Reputation: 4030

obj1 = [
          {
            "code": "in_today",
            "text": "Today"
          },
          {
            "code": "in_week",
            "text": "This week"
          },
          {
            "code": "in_month",
            "text": "This month"
          },
          {
            "code": "normal",
            "text": "Other"
          }
        ]

obj2 = ["in_today", "in_week", "normal"];
    
    obj1.forEach( function(elem){

     if( obj2.indexOf(elem.code) > -1)
       elem.selected = true;
     else
       elem.selected = false;

    });
    
    console.log( JSON.stringify(obj1) );

Upvotes: 0

Cassandra Fox
Cassandra Fox

Reputation: 66

You'll want to do something like this:

for (var i = 0; i < obj1.length; ++i) {
  if (obj2.indexOf(obj1[i].code) == -1) {
    obj1[i].selected = false;
  } else {
    obj1[i].selected = true;
  }
}

Essentially, you just loop over obj1, then check if the value of obj1.code is present in obj2, then set selected accordingly.

Upvotes: 0

kind user
kind user

Reputation: 41913

Simple&quick solution using Array#forEach.

var obj1 = [{"code":"in_today","text":"Today"},{"code":"in_week","text":"This week"},{"code":"in_month","text":"This month"},{"code":"normal","text":"Other"}],
    obj2 = ["in_today", "in_week", "normal"];

    obj1.forEach(v => v.selected = obj2.indexOf(v.code) > -1);

    console.log(obj1);

Upvotes: 0

Rob M.
Rob M.

Reputation: 36521

You can use Array.map to transform your objects based on whether their code is in the obj2 array:

var obj1 = [
  {
    "code": "in_today",
    "text": "Today"
  },
  {
    "code": "in_week",
    "text": "This week"
  },
  {
    "code": "in_month",
    "text": "This month"
  },
  {
    "code": "normal",
    "text": "Other"
  }
]

var obj2 = ["in_today", "in_week", "normal"]

var newObject = obj1.map(function(obj) {
  if (obj2.indexOf(obj.code) > -1) {
    obj.selected = true;
  } else {
    obj.selected = false;
  }
  return obj;
})

console.log(newObject)

Or a bit simpler if ES6 is available:

const newObject = obj1.map((obj) => {
  obj.selected = obj2.includes(obj.code);
  return obj;
})

Upvotes: 1

Related Questions