Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

javascript - 对象的映射功能(而不是数组)(map function for objects (instead of arrays))

I have an object:

(我有一个对象:)

myObject = { 'a': 1, 'b': 2, 'c': 3 }

I am looking for a native method, similar to Array.prototype.map that would be used as follows:

(我正在寻找类似于Array.prototype.map的本机方法,该方法将按以下方式使用:)

newObject = myObject.map(function (value, label) {
    return value * value;
});

// newObject is now { 'a': 1, 'b': 4, 'c': 9 }

Does JavaScript have such a map function for objects?

(JavaScript是否为对象提供了这种map功能?)

(I want this for Node.JS, so I don't care about cross-browser issues.)

((我想为Node.JS使用它,所以我不在乎跨浏览器的问题。))

  ask by Randomblue translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no native map to the Object object, but how about this:

(没有到Object对象的本地map ,但是如何处理:)

 var myObject = { 'a': 1, 'b': 2, 'c': 3 }; Object.keys(myObject).map(function(key, index) { myObject[key] *= 2; }); console.log(myObject); // => { 'a': 2, 'b': 4, 'c': 6 } 

But you could easily iterate over an object using for ... in :

(但是您可以使用for ... in以下对象中轻松迭代对象:)

 var myObject = { 'a': 1, 'b': 2, 'c': 3 }; for (var key in myObject) { if (myObject.hasOwnProperty(key)) { myObject[key] *= 2; } } console.log(myObject); // { 'a': 2, 'b': 4, 'c': 6 } 

Update

(更新资料)

A lot of people are mentioning that the previous methods do not return a new object, but rather operate on the object itself.

(很多人提到,以前的方法不会返回新对象,而是对对象本身进行操作。)

For that matter I wanted to add another solution that returns a new object and leaves the original object as it is:

(为此,我想添加另一个解决方案,该解决方案返回一个新对象并保留原始对象不变:)

 var myObject = { 'a': 1, 'b': 2, 'c': 3 }; // returns a new object with the values at each key mapped using mapFn(value) function objectMap(object, mapFn) { return Object.keys(object).reduce(function(result, key) { result[key] = mapFn(object[key]) return result }, {}) } var newObject = objectMap(myObject, function(value) { return value * 2 }) console.log(newObject); // => { 'a': 2, 'b': 4, 'c': 6 } console.log(myObject); // => { 'a': 1, 'b': 2, 'c': 3 } 

Array.prototype.reduce reduces an array to a single value by somewhat merging the previous value with the current.

(Array.prototype.reduce通过将先前值与当前值进行某种程度的合并,将数组减少为单个值。)

The chain is initialized by an empty object {} .

(该链由一个空对象{}初始化。)

On every iteration a new key of myObject is added with twice the key as the value.

(每次迭代时,都会添加myObject的新键,并将键的值加倍。)

Update

(更新资料)

With new ES6 features, there is a more elegant way to express objectMap .

(借助ES6的新功能,有一种更优雅的方式来表达objectMap 。)

 const objectMap = (obj, fn) => Object.fromEntries( Object.entries(obj).map( ([k, v], i) => [k, fn(v, k, i)] ) ) const myObject = { a: 1, b: 2, c: 3 } console.log(objectMap(myObject, v => 2 * v)) 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...