【问题标题】:Get appropriate objects from array [duplicate]从数组中获取适当的对象[重复]
【发布时间】:2020-04-05 18:53:34
【问题描述】:

我创建了一个如下所示的数组:

arrayObject = [{one: "1", two: "2", three: "3", four: "4", five: "5"}, 
               {one: "1", two: "2", three: "3", four: "4", five: "5"}];

如何获取/过滤该数组,使其仅包含“一”、“二”和“五”元素的 arrayObject,如下所示:

arrayObject = [{one: "1", two: "2", five: "5"}, 
               {one: "1", two: "2",  five: "5"}];

最好的方法是什么?

【问题讨论】:

  • 您是要获取只有 3 个键的对象还是将对象更改为只有 3 个键?
  • 您是要根据键基还是值基来获取数组对象?
  • 我想把 arrya 改成 3 个键。
  • arrayObject.map(({ three, four, ...rest }) => rest)

标签: javascript arrays object


【解决方案1】:

您可以只映射想要的属性,例如解构并从变量中获取新对象。

var array = [{ one: "1", two: "2", three: "3", four: "4", five: "5" }, { one: "1", two: "2", three: "3", four: "4", five: "5" }],
    result = array.map(({ one, two, five }) => ({ one, two, five }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    【解决方案2】:

    如果您有一系列想要保留的项目,可以使用Object.fromEntries().map()。通过将要保留的项目数组(这里我称之为keep)映射到键值对数组(即:[[key, value], ...])。然后,您可以使用Object.fromEntries() 将给定数组转换为如下对象:

    const keep = ["one", "two", "five"];
    const arrayObject = [{ one: "1", two: "2", three: "3", four: "4", five: "5" }, { one: "1", two: "2", three: "3", four: "4", five: "5" } ];
    
    const res = arrayObject.map(
      obj => Object.fromEntries(keep.map(key => [key, obj[key]])
    ));
    console.log(res);

    请注意,Object.fromEntries() 的浏览器支持有限,如果您需要更好的浏览器支持,您可以使用.reduce() 像这样:

    const keep = ["one", "two", "five"];
    const arrayObject = [{ one: "1", two: "2", three: "3", four: "4", five: "5" }, { one: "1", two: "2", three: "3", four: "4", five: "5" } ];
    const res = arrayObject.map(
      obj => keep.map(key => [key, obj[key]]).reduce((acc, [k, v]) => ({...acc, [k]: v}), {})
    );
    console.log(res);

    【讨论】:

      【解决方案3】:

      您可以像下面那样映射和使用传播

      arrayObject = [{one: "1", two: "2", three: "3", four: "4", five: "5"}, 
                     {one: "1", two: "2", three: "3", four: "4", five: "5"}];
                     
      let filtered=arrayObject.map(a=>{
      
        const {three,four,...rest}=a;
        return rest;
      });
      console.log(filtered);

      【讨论】:

      • he 知道原因(他的答案在这里被否决,然后被删除)
      【解决方案4】:

      function extractObjectWithKeys(objects, keys) {
        return objects.reduce((acc, elem) => {
          let newObj = {};
          let filteredKeys = Object.keys(elem).filter(k => keys.includes(k));
          filteredKeys.forEach(key => {
            newObj[key] = elem[key];
          });
          return acc.concat(newObj);
        }, []);
      }
      
      const arrayObject = [
        {one: "1", two: "2", three: "3", four: "4", five: "5"}, 
        {one: "1", two: "2", three: "3", four: "4", five: "5"}
      ];
      
      //Usage:
      extractObjectWithKeys(arrayObject, ["one", "two", "five"])

      【讨论】:

        猜你喜欢
        • 2019-06-28
        • 2021-11-17
        • 1970-01-01
        • 2013-04-09
        • 2020-11-24
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 2019-05-11
        相关资源
        最近更新 更多