最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Iterating objects in es6 and returning new object - Stack Overflow

programmeradmin7浏览0评论

Using es6 javascript is it possible to iterate over an object and return a new object. For example:

const people = {
    'Sally': {
        age: 22,
        sex: 'female',
    },
    'John': {
        age: 64,
        sex: 'male',
    },
    'Sam': {
        age: 12,
        sex: 'female',
    },      
};

const ages = people.someEs6IteratingObjectFunction((index, person) => {
    return { Object.keys(people)[index]: person.age };
});

console.log(ages); // { 'Sally': 22, 'John': 64, 'Sam': 12, }   

Using es6 javascript is it possible to iterate over an object and return a new object. For example:

const people = {
    'Sally': {
        age: 22,
        sex: 'female',
    },
    'John': {
        age: 64,
        sex: 'male',
    },
    'Sam': {
        age: 12,
        sex: 'female',
    },      
};

const ages = people.someEs6IteratingObjectFunction((index, person) => {
    return { Object.keys(people)[index]: person.age };
});

console.log(ages); // { 'Sally': 22, 'John': 64, 'Sam': 12, }   
Share Improve this question edited Apr 22, 2016 at 1:48 nem035 35.5k6 gold badges92 silver badges104 bronze badges asked Apr 22, 2016 at 1:14 AndrewMcLaganAndrewMcLagan 14k25 gold badges96 silver badges166 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 8

You can use reduce method of array prototype. It also works in es5.

const people = {
    'Sally': {
        age: 22,
        sex: 'female',
    },
    'John': {
        age: 64,
        sex: 'male',
    },
    'Sam': {
        age: 12,
        sex: 'female',
    },      
};

let result = Object.keys(people).reduce(function(r, name) {
  return r[name] = people[name].age, r;
}, {});

document.write(['<pre>', JSON.stringify(result, 0, 3), '</pre>'].join(''));

One option if you're not super concerned about potential performance:

const ages = people.reduce(
    (obj, name) => Object.assign(obj, {[name]: obj[name].age}), {});

or maybe

const ages = Object.entries(people).reduce(
    (obj, [name, info]) => Object.assign(obj, {[name]: info.age}), {});

Or make an ES6 Map instead of using an Object:

const ages = new Map(Object.entries(people).map(
    ([name, info]) => [name, info.age])))

Here is one way to do it, I wish I could take credit for this, it is from Axel Rauschmayer's blog, he has several other amazing blog posts here: http://www.2ality./2015/02/es6-iteration.html

Here is a JSBin with the code below:

 function objectEntries(obj) {
        let index = 0;
        let propKeys = Reflect.ownKeys(obj);
        return {
            [Symbol.iterator]() {
                return this;
            },
            next() {
                if (index < propKeys.length) {
                    let key = propKeys[index];
                    index++;
                    return { value: [key, obj[key]] };
                } else {
                    return { done: true };
                }
            }
        };
    }

    let obj = { first: 'Jane', last: 'Doe' };
    for (let [key,value] of objectEntries(obj)) {
        console.log(`${key}: ${value}`);
    }

This is even easier if you use generators: http://www.2ality./2015/03/es6-generators.html

Unfortunately, there's isn't an equivalent of Array.map for objects in ES6.

Maybe you can make a helper function to mimic this. It would iterate over your object, calling the callback for each key with the value at that key, and replacing that value with whatever the callback returns. Something like:

const objectMap = (obj, cb) => 
  Object.keys(obj).reduce((ppl, k) =>
    Object.assign(ppl, {
      [k]: cb(obj[k])
    }), {}
  );

And then use it like:

const newPeople = objectMap(people, val => val.age);
console.log(newPeople); // Object {Sally: 22, John: 64, Sam: 12}

Running example:

const people = {
  'Sally': {
    age: 22,
    sex: 'female',
  },
  'John': {
    age: 64,
    sex: 'male',
  },
  'Sam': {
    age: 12,
    sex: 'female',
  },
};

const objectMap = (obj, cb) => 
  Object
  .keys(obj)
  .reduce(
    (ppl, k) =>
    Object.assign(ppl, {
      [k]: cb(obj[k])
    }), {}
  );

const newPeople = objectMap(people, val => val.age);

console.log('---- People ----');
console.dir(people);

console.log('---- New People ---- ');
console.dir(newPeople);

Maybe you can use for...of?

 const people = { 'Sally': { age: 22, sex: 'female', }, 'John': { age: 64, sex: 'male', }, 'Sam': { age: 12, sex: 'female', },
};

const ages = {};
for(let index of people) {
  ages[key] = people[key].age;
}
console.log(ages); // { 'Sally': 22, 'John': 64, 'Sam': 12, }
发布评论

评论列表(0)

  1. 暂无评论