1

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!

1
  • Is the format of the first object fixed or are you creating it yourself? Commented Apr 9, 2017 at 17:47

4 Answers 4

1

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;
})
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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

1 Comment

it's a snippet now, runs as expected.

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.