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",
}
我将此对象用作日历的提要,其中 属性 名称是日期,属性 值 is/are 日期事件,我的解决方案无法处理两个具有相同名称的属性.此提要由模板引擎生成,它不能仅在呈现视图时以这种方式呈现(对于事件的每个上下文,它向对象添加一行)。
对于了解 Kentico CMS 的人,我正在使用具有效果的中继器来构建此对象,其中“var object = {”是之前的 html 信封,“}”是之后的 html 信封。
首先,您的代码不正确,因为如果名称相同,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;
对于第一次编辑,我建议您将对象更改为其他形式
来自
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; }
我正在尝试找到一些优雅的解决方案,如果名称相同,如何合并对象属性的值。
示例:
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",
}
我将此对象用作日历的提要,其中 属性 名称是日期,属性 值 is/are 日期事件,我的解决方案无法处理两个具有相同名称的属性.此提要由模板引擎生成,它不能仅在呈现视图时以这种方式呈现(对于事件的每个上下文,它向对象添加一行)。
对于了解 Kentico CMS 的人,我正在使用具有效果的中继器来构建此对象,其中“var object = {”是之前的 html 信封,“}”是之后的 html 信封。
首先,您的代码不正确,因为如果名称相同,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;
对于第一次编辑,我建议您将对象更改为其他形式 来自
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; }