Javascript - 合并对象的属性

时间:2017-10-28 10:58:13

标签: javascript kentico

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

示例:

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

3 个答案:

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

答案 1 :(得分: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; }




答案 2 :(得分:-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;