合并对象属性并使用不同的名称分配同级属性

时间:2018-05-21 13:04:23

标签: javascript reactjs ecmascript-6

我有一个带有符号的数组,这些符号有时会重复。 他们的兄弟姐妹是不同的市场a,b,c .... 现在我需要合并所有重复的符号,并将市场附加到它上面。

const theArray = [
      {
        "symbol": "CAT",
        "market_a": "20"
      },
      {
        "symbol": "DOG",
        "market_a": "40"
      },
      {
        "symbol": "DUCK",
        "market_a": "15"
      },
      {
        "symbol": "CAT",
        "market_b": "60"
      },
      {
        "symbol": "DOG",
        "market_b": "55"
      },
      {
        "symbol": "FISH",
        "market_b": "40"
      },
      {
        "symbol": "CAT",
        "market_c": "65"
      },
      {
        "symbol": "DOG",
        "market_c": "20"
      },
      {
        "symbol": "FISH",
        "market_c": "70"
      }
]

等。

它应该像这样合并:

{
  "symbol": "CAT",
  "market_a: "20",
  "market_b: "60",
  "market_c: "65"
},
{
  "symbol": "DOG",
  "market_a": "40",
  "market_b": "55",
  "market_c": "20",
},
{
  "symbol": "FISH",
  "market_b": "40",
  "market_c": "70"
}

等。

到目前为止我尝试了什么:

function groupBy(arr, p) {
  return arr.reduce(function (acc, obj) {
    var key = obj[p];
    if (!acc[key]) {
      acc[key] = [];
    }
    acc[key].push(obj);
    return acc;
  }, {});
}

var final = groupBy(theArray, 'symbol');

console.log(final)

我还想学习一些ES6实践/箭头函数用法,因为我想在React中使用它来获取表行内的JSX输出。 e.g:

var market = formattedArray.map((i, k) => {
      return (
        <tr key={k}>
          <td className="td-symbol">{i.symbol}</td>
          <td className="td-price">{i.market_a}</td>
          <td className="td-price">{i.market_b}</td>
          <td className="td-price">{i.market_c}</td>
        </tr>
      )
    })

4 个答案:

答案 0 :(得分:1)

 function mergeBy(arr, key) {
   const result = [], hash = {};
   for(const {[key]: id, ...rest} of arr) {
     if(!hash[id]) result.push(hash[id] = {[key]: id});
     Object.assign(hash[id], rest);
  }
 return result;
}

只需使用Object.assign合并对象。

答案 1 :(得分:1)

&#13;
&#13;
var a = [{
        "symbol": "CAT",
        "market_a": "20"
    },
    {
        "symbol": "DOG",
        "market_a": "40"
    },
    {
        "symbol": "DUCK",
        "market_a": "15"
    },
    {
        "symbol": "CAT",
        "market_b": "60"
    },
    {
        "symbol": "DOG",
        "market_b": "55"
    },
    {
        "symbol": "FISH",
        "market_b": "40"
    },
    {
        "symbol": "CAT",
        "market_c": "65"
    },
    {
        "symbol": "DOG",
        "market_c": "20"
    },
    {
        "symbol": "fISH",
        "market_c": "70"
    }
];

var result = {};
a.forEach(val => {
    result[val.symbol] = result[val.symbol] || {};
    result[val.symbol] = { ...result[val.symbol],
        ...val
    };
});
result = Object.values(result);
console.log(result);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

你差不多了。您只需使用Object.assign方法即可合并 properties

&#13;
&#13;
let arr = [{ "symbol": "CAT", "market_a": "20" }, { "symbol": "DOG", "market_a": "40" }, { "symbol": "DUCK", "market_a": "15" }, { "symbol": "CAT", "market_b": "60" }, { "symbol": "DOG", "market_b": "55" }, { "symbol": "FISH", "market_b": "40" }, { "symbol": "CAT", "market_c": "65" }, { "symbol": "DOG", "market_c": "20" }, { "symbol": "fISH", "market_c": "70" }]
  
function groupBy(arr, p) {
  return arr.reduce(function (acc, obj) {
    var key = obj[p];
    if (!acc[key]) {
      acc[key] = {};
    }
    acc[key] = Object.assign(acc[key], obj);
    return acc;
  }, {});
}

var final = groupBy(arr, 'symbol');

console.log(final)
&#13;
&#13;
&#13;

答案 3 :(得分:1)

尝试以下

&#13;
&#13;
xml_rdd = sc.newAPIHadoopFile('file:///tmp/mods/*.xml','com.databricks.spark.xml.XmlInputFormat','org.apache.hadoop.io.LongWritable','org.apache.hadoop.io.Text',conf={'xmlinput.start':'<mods:mods>','xmlinput.end':'</mods:mods>','xmlinput.encoding': 'utf-8'})
&#13;
&#13;
&#13;

供参考,Object.assignArray.reduceObject.values