简体   繁体   English

使用underscore.js进行对象转换

[英]object transformation with underscore.js

For whatever reason, I have quite a bit of difficulty when trying to transform objects and arrays. 无论出于何种原因,在尝试转换对象和数组时我都会遇到很多困难。

The current data I have is stored in a mongodb collection and looks like this: 我拥有的当前数据存储在mongodb集合中,如下所示:

[{ "_id" : 1, "name" : "someName", "colorName" : "Pink"},
{ "_id" : 2, "name" : "someName2", "colorName" : "RoyalBlue"},
{ "_id" : 3, "name" : "aThirdOne", "colorName" : "Gold"},
{ "_id" : 4, "name" : "oneMore", "colorName" : "LightGreen"}]

I need to get either the following two arrays, but would like to know how to get both. 我需要得到下面的两个数组,但想知道如何让两者。

[{ "value" : 1, "label" : "someName"},
{ "value" : 2, "label" : "someName2"},
{ "value" : 3, "label" : "aThirdOne"},
{ "value" : 4, "label" : "oneMore"]

[{1 : "someName"},
{2 : "someName2"},
{3 : "aThirdOne"},
{4 : "oneMore"}]

I know it is probably something with _.map , but I am not sure why I can't figure it out. 我知道_.map可能与此_.map ,但是我不确定为什么无法弄清楚。 Please advise. 请指教。

You don't really need Underscore for this, it's just normal Array.prototype.map method usage (although Underscore also provides this method): 您实际上并不需要Underscore,这只是Array.prototype.map方法的常规用法(尽管Underscore也提供了此方法):

 var data = [{ "_id" : 1, "name" : "someName", "colorName" : "Pink"}, { "_id" : 2, "name" : "someName2", "colorName" : "RoyalBlue"}, { "_id" : 3, "name" : "aThirdOne", "colorName" : "Gold"}, { "_id" : 4, "name" : "oneMore", "colorName" : "LightGreen"}]; var result = data.map(function(el, i) { return {value: el._id, label: el.name}; }); document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 4) + '</pre>'; 

Also note, that your second data structure doesn't make sense because it's irregular, so don't use it, it's not very convenient at all. 还要注意,您的第二个数据结构没有意义,因为它是不规则的,所以不要使用它,这根本不是很方便。

And here is corresponding Underscore version: 这是对应的下划线版本:

// Or Underscore version
var result = _.map(data, function(el) {
    return {value: el._id, label: el.name};
});

In plain Javascript you can use Array.prototype.forEach() for iteration througt the array and assembling the objects and push it to the wanted results. 在普通的Javascript中,可以使用Array.prototype.forEach()遍历数组并组装对象并将其推入所需的结果。

The forEach() method executes a provided function once per array element. forEach()方法每个数组元素执行一次提供的函数。

 var array = [{ "_id": 1, "name": "someName", "colorName": "Pink" }, { "_id": 2, "name": "someName2", "colorName": "RoyalBlue" }, { "_id": 3, "name": "aThirdOne", "colorName": "Gold" }, { "_id": 4, "name": "oneMore", "colorName": "LightGreen" }], result1 = [], result2 = []; array.forEach(function (a) { result1.push({ value: a._id, label: a.name }); var o = {}; o[a._id] = a.name result2.push(o); }); document.write('<pre>' + JSON.stringify(result1, 0, 4) + '</pre>'); document.write('<pre>' + JSON.stringify(result2, 0, 4) + '</pre>'); 

use: 采用:

var newObject = _.map(yourObject, function(val) {
return {
    "value": val._id,
    "label": val.name
}});

And the same concept for the other result you're interested in . 和您感兴趣的其他结果的概念相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM