1

I have this data

var game = [
  {
    'userType' : 'VIP',
    'data' : [{
      'name' : 'John'
    }]
  },
  {'userType' : 'VIP',
    'data' : [{
      'name' : 'Michelle'
    }]
  }];

var newArr = { isActive: true };

I've tried to do this

game.push.apply(game[0], newArr);

And there's no newArr when i did the console log

enter image description here

Am I missing something here? How to append newArr to the first array of game? (game[0])

Thanks guys

1 Answer 1

3

You are adding properties of one object - newArr to another - game[0] and for that you can use Object.assign().

var game = [{
  'userType': 'VIP',
  'data': [{
    'name': 'John'
  }]
}, {
  'userType': 'VIP',
  'data': [{
    'name': 'Michelle'
  }]
}];

var newArr = {isActive: true};
Object.assign(game[0], newArr);

console.log(game)

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

1 Comment

thats what im looking for!!!!! deserved marked as answer, cause you're the first. Have to wait 9 mins, btw, will do it later. Thanks man

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.