簡體   English   中英

Javascript-合並對象的屬性

[英]Javascript - merge properties of an object

我試圖找出一些優雅的解決方案,如果名稱相同,則如何合並對象屬性的值。

例:

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信封。

對於第一次編輯,建議您將對象從

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"}] 

如果我的評論中的問題回答“ 是” ,則可能提供了一種可行的方法...

 // 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; } 

首先,您的代碼不正確,因為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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM