admin管理员组

文章数量:1277896

I currently have this Object:

schoolsObject = [{
  "college_1":
    {
      "id":"college_1",
      "location":"Victoria",
      "name":"College One"
    },
  "college_2":
    {
      "id":"college_2",
      "location":"Tasmania",
      "name":"College Two"
    }
  }];

I want to remove the top level keys ie. college_1, college_2 and 'flatten' the object out like this, so I have no 'top level' keys:

flatSchoolsObject = 
    [{
      "id":"college_1",
      "location":"Victoria",
      "name":"College One"
    },
    {
      "id":"college_2",
      "location":"Tasmania",
      "name":"College Two"
    }];

Here is my latest attempt, I've made a lot of different try's but have not been documenting them:

// schoolIDs = Object.keys(schoolsObject);
var schools = {};

for(var i=0; i<Object.keys(schoolsObject).length; i++){
  for (var property in schoolsObject) {
    if (schoolsObject.hasOwnProperty(property)) {
      schools[i] = {
        'id': schoolsObject[property]['id'],
        'name' : schoolsObject[property]['name'],
        'location': schoolsObject[property]['location'],
      };
    }
  }
}
console.log(schools)

Obviously this one is not what I'm after as it leaves me with Object {0: Object, 1: Object}.

Is what I want to do here possible or am I looking at it the wrong way?

I currently have this Object:

schoolsObject = [{
  "college_1":
    {
      "id":"college_1",
      "location":"Victoria",
      "name":"College One"
    },
  "college_2":
    {
      "id":"college_2",
      "location":"Tasmania",
      "name":"College Two"
    }
  }];

I want to remove the top level keys ie. college_1, college_2 and 'flatten' the object out like this, so I have no 'top level' keys:

flatSchoolsObject = 
    [{
      "id":"college_1",
      "location":"Victoria",
      "name":"College One"
    },
    {
      "id":"college_2",
      "location":"Tasmania",
      "name":"College Two"
    }];

Here is my latest attempt, I've made a lot of different try's but have not been documenting them:

// schoolIDs = Object.keys(schoolsObject);
var schools = {};

for(var i=0; i<Object.keys(schoolsObject).length; i++){
  for (var property in schoolsObject) {
    if (schoolsObject.hasOwnProperty(property)) {
      schools[i] = {
        'id': schoolsObject[property]['id'],
        'name' : schoolsObject[property]['name'],
        'location': schoolsObject[property]['location'],
      };
    }
  }
}
console.log(schools)

Obviously this one is not what I'm after as it leaves me with Object {0: Object, 1: Object}.

Is what I want to do here possible or am I looking at it the wrong way?

Share Improve this question asked Jul 28, 2016 at 12:33 pi_ronpi_ron 2104 silver badges11 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

Given Object:

schoolsObject = [{
    "college_1":{
        "id":"college_1",
        "location":"Victoria",
        "name":"College One"
    },
    "college_2":{
        "id":"college_2",
        "location":"Tasmania",
        "name":"College Two"
    }
}];

Solution:

Object.values(schoolsObject[0]);

Result:

[{
    "id":"college_1",
    "location":"Victoria",
    "name":"College One"
},{
    "id":"college_2",
    "location":"Tasmania",
    "name":"College Two"
}]

(Codewise) simplest solution could be using a bination of Object.keys() and Array.map():

flatSchoolsObject = Object.keys( schoolsObject[0] )
                          .map( ( key ) => schoolsObject[0][ key ] );

If the schoolsObject array has more entries, the code would have to be slightly adjusted:

let step1 = schoolsObject.map( ( el ) => {
              return Object.keys( schoolsObject[0] )
                           .map( ( key ) => schoolsObject[0][ key ] );
            })
flatSchoolsObject = [].concat.apply( [], step1 );

(the step1 variable is just introduced for readability reasons.)

You need to concat the result of extracting values from each item in schoolObject

flatSchoolsObject  = [].concat.call(
    schoolsObject.map(function(item) {
       return Object.keys(item).map(function(key) {
          return item[key];
       })
    })    
)

or using Array.prototype.reduce

flatSchoolsObject = schoolsObject.reduce(function(acc, item) {
  return acc.concat(Object.keys(item).map(function(key){
     return item[key]
  })
}, [])

You can use Array#map on the result of Object.keys to do it. Since you have just a single object in the array, we do it like this:

schoolsObject = Object.keys(schoolsObject[0]).map(function(key) {
  return schoolsObject[0][key];
});

Live example:

var schoolsObject = [
  {
  "college_1": {
    "id": "college_1",
    "location": "Victoria",
    "name": "College One"
  },
  "college_2": {
    "id": "college_2",
    "location": "Tasmania",
    "name": "College Two"
  }
}];
schoolsObject = Object.keys(schoolsObject[0]).map(function(key) {
  return schoolsObject[0][key];
});
console.log(schoolsObject);

With ES2015+ you could use an arrow function to make that shorter:

schoolsObject = Object.keys(schoolsObject[0]).map(key => schoolsObject[0][key]);

// Code goes here
var schoolsObject = [{
  "college_1":
    {
      "id":"college_1",
      "location":"Victoria",
      "name":"College One"
    },
  "college_2":
    {
      "id":"college_2",
      "location":"Tasmania",
      "name":"College Two"
    }
  }];
  var result = Object.keys(schoolsObject[0]).map(function(key){
    return schoolsObject[0][key];
  })

console.log(result);

other version

var schoolsObject = [{
  "college_1": {
    "id": "college_1",
    "location": "Victoria",
    "name": "College One"
  },
  "college_2": {
    "id": "college_2",
    "location": "Tasmania",
    "name": "College Two"
  }
}];
var result = [];

for (var property in schoolsObject[0]) {
  if (schoolsObject[0].hasOwnProperty(property)) {
    result.push(schoolsObject[0][property]);
  }
}

console.log(result);

本文标签: How can I remove the parent keys from a javascript ObjectStack Overflow