2017-10-28 110 views
1

我想找出一些优雅的解决方案,如果名称相同,如何合并对象属性的值。Javascript - 合并属性的对象

实施例:

var object = { 
"10-10-2017": "Black friday", 
"11-09-2017": "Some holiday", 
"10-10-2017": "Fathers day" 
} 

合并到:

var object = { 
"10-10-2017": "Black friday Fathers day", 
"11-09-2017": "Some holiday", 
} 

我使用此对象作为日历进料,其中属性名称是日期和属性值是/是日期和我的解决方案的事件无法处理具有相同名称的两个属性。此提要是由模板引擎生成的,并且它不能只在呈现视图时以这种方式呈现它(对于事件的每个上下文,它都会向对象添加一行)。

对于那些了解Kentico CMS的人,我使用中继器来生成此对象,其中“var object = {”是之前的html信封,“}”是之后的html信封。

+6

你的第一个对象是不是有效的对象。你不能有两个同名的键。 – Xufox

+1

^^对象初始化器是有效的,但对象只有两个属性'10-10-2017' ='“父亲节”和'11-09-2017' ='“一些假期”''。如果您在初始化程序中多次列出相同的属性,那么最后一个会胜出。所以我们不能帮你,因为所描述的源对象只有两个,而不是三个属性。没有什么可以合并的。 –

+0

你的数据是如何进入这个状态的?您可能需要在创建任何对象之前解决重复问题。 –

回答

0

对于第一个编辑,我建议你从

var object = { 
"10-10-2017": "Black friday", 
"11-09-2017": "Some holiday", 
"10-10-2017": "Fathers day" 
} 

改变你的对象为其他形式 到

var object =[ 
{"date":"10-10-2017","value":"Black friday"}, 
{"date":"11-09-2017","value":"Some holiday"}, 
{"date":"10-10-2017","value":"Fathers day"} 
] 

如果这里可以帮助你的工作液

var object = [{date:"10-10-2017",value:"Black friday"},{date:"11-09-2017",value:"Some holiday"},{date:"10-10-2017",value:"Fathers day"}]; 
 

 
var output =[]; 
 

 
object.forEach(function(value) { 
 
    var existing = output.filter(function(v, i) { 
 
    return v.date == value.date; 
 
    }); 
 
    if (existing.length) { 
 
    var existingIndex = output.indexOf(existing[0]); 
 
    output[existingIndex].value += ' '+value.value 
 
    
 
    } else { 
 
    if (typeof value.value == 'string') 
 
     value.value = value.value; 
 
    output.push(value); 
 
    } 
 
}); 
 

 
console.log(JSON.stringify(output)); //returns [{"date":"10-10-2017","value":"Black friday Fathers day"},{"date":"11-09-2017","value":"Some holiday"}]

-3

首先你的代码是不正确的,因为如果名字相同,JS只会拾取最后一个道具。

> var object = { 
... "10-10-2017": "Black friday", 
... "11-09-2017": "Some holiday", 
... "10-10-2017": "Fathers day" 
... } 
undefined 
> object 
{ '10-10-2017': 'Fathers day', '11-09-2017': 'Some holiday' } 
> 

对于额外的道具,有一个解决方案,希望这可以帮助你,例如您

object[newProperty] = (object[newProperty] || '') + newValue; 
0

万一question from my comment答案与,这可能提供了一个工作方法......

// BEGIN of CMS templating ... 
 

 
var descriptorList = []; 
 

 
// programmatically push into a list of property descriptors, 
 
// each descriptor holding just one key value pair. 
 

 

 
    // +++ BEGIN iteration here ... 
 

 
    descriptorList.push({ "10-10-2017": "Black friday" }); 
 
    // ... 
 
    // ... keep looping ... "Kentico CMS repeater"? 
 

 

 
    // for the sake of providing a working example 
 
    // there will be some more descriptors pushed ... 
 

 
    descriptorList.push({ "11-09-2017": "Some holiday" }); 
 
    descriptorList.push({ "10-10-2017": "Fathers day" }); 
 

 
    // ... END of iteration. +++ 
 

 

 
function mergeObjectsAndConcatSamePropertyStringTypes(a, b) { 
 
    var 
 
    type = Object.assign({}, a, b);  // - start with a raw merged type (merger). 
 

 
    Object.keys(b).filter(function (key) { // - collect `key` duplicates. 
 
    return (key in a); 
 
    }).forEach(function (key) {    // - for each duplicate `key` that targets 
 
    var aValue = a[key];     // a string type at each object, do concat 
 
    var bValue = b[key];     // this values and assign it to the merger. 
 
    if ((typeof aValue == 'string') &&/*||*/ (typeof bValue == 'string')) { 
 
     type[key] = [aValue, bValue].join(' '); 
 
    } 
 
    }); 
 

 
    return type; 
 
} 
 

 
// assembling of the desired type ... 
 
var type = descriptorList.reduce(mergeObjectsAndConcatSamePropertyStringTypes, {}); 
 

 
// END of CMS templating ... 
 

 

 
console.log('descriptorList : ', descriptorList); 
 
console.log('type : ', type);
.as-console-wrapper { max-height: 100%!important; top: 0; }