是否存在转换此结构的任何underscorejs函数(或native / custom js函数):
(请查看真实示例@Jsfiddle)
//Simple version, check Jsfiddle for the complete one
var object = {
"users" : [
{
name : "name1"
},
{
name : "name2",
}
]
}
到另一个像这样的对象:
//Simple version, check Jsfiddle for the complete one
var object2 = {
users : {
name : "name1, name2"
}
}
答案 0 :(得分:1)
这与你的小提琴相同:
function getType(val){
if(Object.prototype.toString.call(val) == '[object Array]') return 'array';
if(val !== null && typeof val === 'object') return 'object';
return 'other';
}
function deepReduce(object){
var obj={};
function travel(object, key, obj){
if( getType(object) == 'array'){ // if its array, travel
for(var i=0;i<object.length;i++){
travel(object[i],key,obj)
}
}else if(getType(object)=='object'){ // if its object
for(var i in object){
if(getType(object[i])!='other'){ // if property is object, keep travel
if(!obj.hasOwnProperty(i)){
obj[i]={};
}
travel(object[i],i,obj[i])
}else{ // else, set property
obj[i] = (obj[i] == undefined ? object[i] : obj[i]+',' + object[i]) ;
}
}
}
}
travel(object,null, obj);
return obj;
}
var reduced=deepReduce(object)
console.log(reduced)
答案 1 :(得分:0)
您可以使用地图功能
进行操作object2 = {}
object2.name = object.users.map(function(a){
return a.name;
}).join(",");
答案 2 :(得分:0)
也许使用reduce?它应该在ES5中提供。
var users = [{name: 'Joe'}, {name: 'Anna'}];
var names = users.reduce(function(prev, current) {
return prev.name + ' ' + current.name;
});
console.log(names); // "Joe Anna"
让你的小提琴上的这个工作: http://jsfiddle.net/Lun8kx2k/
您也可以减少其他键,或者编写一个递归检查键并减少它们的函数。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
答案 3 :(得分:0)
这是一个工作解决方案,代码行少一点:
Object.deepExtend = function xtend(destination, source) {
for (var property in source) {
if (source[property] && source[property].constructor && source[property].constructor === Object) {
destination[property] = destination[property] || {};
xtend(destination[property], source[property]);
} else {
if (!(destination[property] instanceof Array)) {
destination[property] = destination[property] == null ? source[property] : [destination[property], source[property]].join(",");
} else {
destination[property] = destination[property] == null ? source[property] : destination[property].concat(source[property]);
}
}
}
return destination;
};
Object.deepExtend(object.users[0], object.users[1])
var object = {
"users" : [
{
name : "name1",
gender : "M",
age : "18",
rating : {stars : 5, date : "01.01.2014"},
articles : [
{
title : "article1",
description : "description1"
},
{
title : "article2",
description : "description2"
}
]
},
{
name : "name2",
gender : "F",
age : "19",
rating : {stars : 6, date : "02.01.2014"},
articles : [
{
title : "article3",
description : "description3"
},
{
title : "article4",
description : "description4"
}
]
}
]
}
Object.deepExtend = function xtend(destination, source) {
for (var property in source) {
if (source[property] && source[property].constructor && source[property].constructor === Object) {
destination[property] = destination[property] || {};
xtend(destination[property], source[property]);
} else {
if (!(destination[property] instanceof Array)) {
destination[property] = destination[property] == null ? source[property] : [destination[property], source[property]].join(",");
} else {
destination[property] = destination[property] == null ? source[property] : destination[property].concat(source[property]);
}
}
}
return destination;
};
document.body.appendChild(document.createElement('pre')).innerHTML = (JSON.stringify(Object.deepExtend(object.users[0], object.users[1]), undefined,3));
答案 4 :(得分:0)
这使用lodash(并且只是部分完成)......也许你可以尝试这样的事情:
var object2 = { users: {} };
_.each(['name', 'gender', 'age'], function (prop) {
object2.users[prop] = _.pluck(object.users, prop).join(', ');
});
// returns Object { users: { age: "18, 19", gender: "M, F", name: "name1, name2" }}
<强> DEMO 强>