仅返回唯一元素的嵌套数组过滤器

时间:2019-03-19 08:11:25

标签: javascript arrays reactjs

{
  list: [
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "RUS",
          "desc": "Russian"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "RUS",
          "desc": "Russian"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "IND",
          "desc": "Indian"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "USA",
          "desc": "American"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "AUS",
          "desc": "Australian"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "JPN",
          "desc": "Japan"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "RUS",
          "desc": "Russian"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "IND",
          "desc": "Indian"
        }
      ]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [
        {
          "code": "RUS",
          "desc": "Russian"
        }
      ]
    }
  ]
}

输出:

Array LanguageCodes[
  {
    "code": "RUS",
    "desc": "Russian"
  },
  {
    "code": "IND",
    "desc": "Indian"
  },
  {
    "code": "USA",
    "desc": "American"
  },
  {
    "code": "AUS",
    "desc": "Australian"
  },
  {
    "code": "JPN",
    "desc": "Japan"
  }
]

我有一个嵌套的数组,代表语言代码。

list数组包含各种对象,它们的语言代码显示在代码片段中。 languageCodes数组包含代码和desc,它们也显示在代码段中。 所以在这里我的要求是过滤数组,输出就像OUTPUT所示的唯一languageCodes数组一样。

5 个答案:

答案 0 :(得分:0)

您可以将reducefilterfor..of组合使用

const obj = {
  list: [{
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "RUS",
        "desc": "Russian"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "RUS",
        "desc": "Russian"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "IND",
        "desc": "Indian"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "USA",
        "desc": "American"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "AUS",
        "desc": "Australian"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "JPN",
        "desc": "Japan"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "RUS",
        "desc": "Russian"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "IND",
        "desc": "Indian"
      }]
    },
    {
      "distance": 1.87,
      "age": "",
      languageCodes: [{
        "code": "RUS",
        "desc": "Russian"
      }]
    }
  ]
};
const output = obj.list.reduce((acc, element) => {
  for (let lang of element.languageCodes) {

    if (!acc.find(l => l.code == lang.code)) {
      acc.push(lang);
    }
  }

  return acc;
}, []);

console.log("output", output);

答案 1 :(得分:0)

使用flatMapfilter

const myObj = {list: [{"distance": 1.87,"age": "",languageCodes: [{"code": "RUS","desc": "Russian"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "RUS","desc": "Russian"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "IND","desc": "Indian"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "USA","desc": "American"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "AUS","desc": "Australian"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "JPN", "desc": "Japan"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "RUS","desc": "Russian"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "IND","desc": "Indian"}]},{"distance": 1.87,"age": "",languageCodes: [{"code": "RUS","desc": "Russian"}]}]};
const LanguageCodes = myObj.list.flatMap(obj => obj.languageCodes).filter((e, i, a) => a.findIndex(({ code, desc }) => code == e.code && desc == e.desc) == i);

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

答案 2 :(得分:0)

尝试

// const arr = inputObj.list;
const arr = [
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'RUS',
        'desc': 'Russian',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'RUS',
        'desc': 'Russian',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'IND',
        'desc': 'Indian',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'USA',
        'desc': 'American',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'AUS',
        'desc': 'Australian',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'JPN',
        'desc': 'Japan',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'RUS',
        'desc': 'Russian',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'IND',
        'desc': 'Indian',
      },
    ],
  },
  {
    'distance': 1.87,
    'age': '',
    languageCodes: [
      {
        'code': 'RUS',
        'desc': 'Russian',
      },
    ],
  },
];

const uniqueLanguageCodes = arr.reduce((accum, current) => {
  current.languageCodes.forEach(codeObj => {
    if (!accum.some(accumElement => accumElement.code === codeObj.code)) {
      accum.push(codeObj);
    }
  });
  return accum;
}, []);

console.log(uniqueLanguageCodes);

答案 3 :(得分:0)

您可以使用Map并拔出codedesc以获得唯一值。

var data = { list: [{ distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "IND", desc: "Indian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "USA", desc: "American" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "AUS", desc: "Australian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "JPN", desc: "Japan" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "IND", desc: "Indian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }] },
    result = Array.from(data.list
        .reduce(
            (m, { languageCodes: { 0: { code, desc } } }) => m.set(code, { code, desc }),
            new Map
        )
        .values()
    );

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

答案 4 :(得分:0)

您可以使用flatMap将所有languageCodes放入单个数组。然后,使用reduceObject.values()获得唯一的语言

const input = { list: [{ distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "IND", desc: "Indian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "USA", desc: "American" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "AUS", desc: "Australian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "JPN", desc: "Japan" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "IND", desc: "Indian" }] }, { distance: 1.87, age: "", languageCodes: [{ code: "RUS", desc: "Russian" }] }] };
 
const merged = input.list.flatMap(a => a.languageCodes)
                         .reduce((acc, lang) => {
                            acc[lang.code] = lang;
                            return acc;
                         }, {})

const output = Object.values(merged)
console.log(output)