繁体   English   中英

如何比较对象数组并返回具有匹配和不匹配键值的 object 数组

[英]How to compare array of objects and return an array of object with match and non match key value

这是我的 Object 数组我想过滤具有匹配和不匹配键值的数据并存储在数组中。 我如何比较这个 object 数组

      const data = [{
        overview: {
          "Engine type": "F8D",
          "Max Power (PS @ rpm)": "47.99 @ 6000",
          "Overall length (mm)": "3445",
          "Overall width (mm)":
            "1490 (without side moulding) / 1515 (with side moulding)",
          "Overall height (mm)": "1475",
          "ABS with EBD": "Yes"
        }
        car_id: "119",
        model_cd: "Alto",
      },
      {
        overview: {
          "Max Power (PS @ rpm)": "54 @ 5678",
          "Overall length (mm)": "3429",
          "Overall width (mm)": "1560",
          "Overall height (mm)": "1541",
          "ABS with EBD": "Yes",
          "Daylight Running Lamps": "Yes"
        },
        car_id: "129",
        model_cd: "Redi-GO",
        model_desc: "Redi-GO",
      }
    ];

我的预期结果如下

[
overview{
      "Engine type": "F8D",
      "Max Power (PS @ rpm)": "54 @ 5678",
      "Overall length (mm)": "3429",
      "Overall width (mm)": "1560",
      "Overall height (mm)": "1541",
      "ABS with EBD": "Yes",
      "Daylight Running Lamps": "Yes"
}
]

谢谢!

考虑使用 jQuery 扩展: https://api.jquery.com/jquery.extend/

例子:

 const myData = [{ overview: { "Engine type": "F8D", "Max Power (PS @ rpm)": "47.99 @ 6000", "Overall length (mm)": "3445", "Overall width (mm)": "1490 (without side moulding) / 1515 (with side moulding)", "Overall height (mm)": "1475", "ABS with EBD": "Yes" }, car_id: "119", model_cd: "Alto", }, { overview: { "Max Power (PS @ rpm)": "54 @ 5678", "Overall length (mm)": "3429", "Overall width (mm)": "1560", "Overall height (mm)": "1541", "ABS with EBD": "Yes", "Daylight Running Lamps": "Yes" }, car_id: "129", model_cd: "Redi-GO", model_desc: "Redi-GO", } ]; function combine(key, data) { var temp = {}; temp[key] = {}; $.each(data, function(i, el) { // Merge myData Item into temp Object temp[key] = $.extend(temp[key], el[key]); }); return [temp]; } console.log(combine("overview", myData));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM