1

I am trying to use the filter function in Angular.js but it doesn't work on objects.

How do I merge a nested object into its parent using Javascript?

e.g. I want to turn this:

{
  "data" : [ {
    "character" : {
      "realm" : 1,
      "displayName" : "John",
    },
    "points" : 1388.0,
    "wins" : 84,
    "losses" : 31
  }, {
    "character" : {
      "realm" : 1,
      "displayName" : "Steven",
    },
    "points" : 1363.0,
    "wins" : 96,
    "losses" : 24
  }, {
    "character" : {
      "realm" : 1,
      "displayName" : "Mark",
    },
    "points" : 1322.0,
    "wins" : 154,
    "losses" : 43
  }
]}

into this:

{
  "data" : [ {
    "realm" : 1,
    "displayName" : "John",
    "points" : 1388.0,
    "wins" : 84,
    "losses" : 31
  }, {
    "realm" : 1,
    "displayName" : "Steven",
    "points" : 1363.0,
    "wins" : 96,
    "losses" : 24
  }, {
    "realm" : 1,
    "displayName" : "Mark",
    "points" : 1322.0,
    "wins" : 154,
    "losses" : 43
  }
]}

Can someone help me out?

2
  • Sounds like you need extend Commented Dec 18, 2014 at 2:55
  • Those are objects, not arrays!!! Not sure how you're trying to use filter on the .data so that it doesn't work? Commented Dec 18, 2014 at 4:16

3 Answers 3

2

You can loop and assign using extend (https://docs.angularjs.org/api/ng/function/angular.extend):

for (var i = 0; i < data.length; i++) {
    angular.extend(data[i], data[i].character) //move properties from character to the parent
    delete data[i].character; //delete the "character" key
}
Sign up to request clarification or add additional context in comments.

Comments

2

Pure Javascript Approach:

var source = {
  "data" : [ {
    "character" : { "realm" : 1, "displayName" : "John" },
    "points" : 1388.0,
    "wins" : 84,
    "losses" : 31
  }, {
    "character" : { "realm" : 1, "displayName" : "Steven" },
    "points" : 1363.0,
    "wins" : 96,
    "losses" : 24
  }, {
    "character" : { "realm" : 1, "displayName" : "Mark" },
    "points" : 1322.0,
    "wins" : 154,
    "losses" : 43
  }
]}

source.data.forEach(function(item) {
    var character = item.character;
    delete item.character;
    for(var prop in character) {
        item[prop] = character[prop];
    }
});

console.log(source);

Comments

0

Use

var obj = {
  "data": [{
    "character": {
      "realm": 1,
      "displayName": "John",
    },
    "points": 1388.0,
    "wins": 84,
    "losses": 31
  }, {
    "character": {
      "realm": 1,
      "displayName": "Steven",
    },
    "points": 1363.0,
    "wins": 96,
    "losses": 24
  }, {
    "character": {
      "realm": 1,
      "displayName": "Mark",
    },
    "points": 1322.0,
    "wins": 154,
    "losses": 43
  }]
};
for (var key in obj.data) {
  obj.data[key].realm = obj.data[key].character.realm; // store the realm key to the obj
  obj.data[key].displayName = obj.data[key].character.displayName; // store the displayName key to the obj
  delete obj.data[key].character; // delete a character key

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.