如何使用 Lodash 遍历具有多个元素的对象数组以创建仅包含重复项的新数组

How to traverse an Array of Objects with multiple elements to make a new array with only the duplicates using Lodash

所以,这是我的情况:

我有一个对象数组,有 3300 多个,可能更少也可能更多。

我做得很好,现在,我想获取并扫描该对象数组,找到所有重复的市场并创建一个新数组,其中仅包含该对象数组中众多元素中的一个。

这是许多对象的简化示例:

[{
  "EL1": "",
  "EL2": "ASDGFDSFFE 1:15",
  "MARKET": "IL",
  "EL3": "CHCHILJH",
  "EL4": "A:B::1243",
  "EL5": "ADFD"
},{
  "EL1": "",
  "EL2": "ASDGFDSFFE 1:15",
  "MARKET": "CA",
  "EL3": "CHCHILJH",
  "EL4": "A:B::1243",
  "EL5": "ADFD"
},{
  "EL1": "",
  "EL2": "ASDGFDSFFE 1:15",
  "MARKET": "ATLANTA GA",
  "EL3": "CHCHILJH",
  "EL4": "A:B::1243",
  "EL5": "ADFD"
},{
  "EL1": "",
  "EL2": "ASDGFDSFFE 1:15",
  "MARKET": "ATLANTA GA",
  "EL3": "CHCHILJH",
  "EL4": "A:B::1243",
  "EL5": "ADFD"
},{
  "EL1": "",
  "EL2": "ASDGFDSFFE 1:15",
  "MARKET": "REGION 5",
  "EL3": "CHCHILJH",
  "EL4": "A:B::1243",
  "EL5": "ADFD"
},{
  "EL1": "",
  "EL2": "ASDGFDSFFE 1:15",
  "MARKET": "REGION 5",
  "EL3": "CHCHILJH",
  "EL4": "A:B::1243",
  "EL5": "ADFD"
}]

我想要的是一个按字母顺序 returns 的新数组:

["ATLANTA GA", "CA", "IL", "REGION 5"]

到目前为止,这是我的代码:

        $scope.nestAssociation = (arr) => {

            let newArr = JSON.parse(arr);

            // console.log("incoming array: ", newArr);

            const uniqueMarketNames = $scope._.uniqBy(newArr.message, 'MARKET'); // removed if had duplicate id
            // TRIED THIS...  
            // const uniqueMarkets = _.uniqWith(
            //  newArr.message,
            //  (MarketsAll) =>
            //    MarketsAll === newArr.message.MARKET
            // );
            
            let uniqueMarkets = $scope._.uniqBy(newArr, function(elem) {
                return JSON.stringify(_.pick(elem, ['a', 'b']));
            });

            console.log("Unique Market names: ", uniqueMarketNames);
            console.log("Unique Markets: ", uniqueMarkets);

            return uniqueMarkets;
        }

我得到的是整个对象块以及 3300 多个对象中的 12 个重复项。

所以,它有效但无效,只给我市场

// mock data
const newArr = {
  message: [{
    "EL1": "",
    "EL2": "ASDGFDSFFE 1:15",
    "MARKET": "IL",
    "EL3": "CHCHILJH",
    "EL4": "A:B::1243",
    "EL5": "ADFD"
  },{
    "EL1": "",
    "EL2": "ASDGFDSFFE 1:15",
    "MARKET": "CA",
    "EL3": "CHCHILJH",
    "EL4": "A:B::1243",
    "EL5": "ADFD"
  },{
    "EL1": "",
    "EL2": "ASDGFDSFFE 1:15",
    "MARKET": "ATLANTA GA",
    "EL3": "CHCHILJH",
    "EL4": "A:B::1243",
    "EL5": "ADFD"
  },{
    "EL1": "",
    "EL2": "ASDGFDSFFE 1:15",
    "MARKET": "ATLANTA GA",
    "EL3": "CHCHILJH",
    "EL4": "A:B::1243",
    "EL5": "ADFD"
  },{
    "EL1": "",
    "EL2": "ASDGFDSFFE 1:15",
    "MARKET": "REGION 5",
    "EL3": "CHCHILJH",
    "EL4": "A:B::1243",
    "EL5": "ADFD"
  },{
    "EL1": "",
    "EL2": "ASDGFDSFFE 1:15",
    "MARKET": "REGION 5",
    "EL3": "CHCHILJH",
    "EL4": "A:B::1243",
    "EL5": "ADFD"
  }]
 };
 
 const uniqueMarkets =
   // convert Set to array
   Array.from(
    new Set(
      // extract markets
      newArr
        .message
        .map(({ MARKET }) => MARKET))
   )
   .sort(); // sort alphabetically 
 
 console.log(uniqueMarkets);

尝试使用 Lodash(uniq 和 map)和 Javascript 排序:

// arr = [{ “EL1”:“”, "EL2": "ASDGFDSFFE 1:15", “市场”:“IL”, “EL3”:“CHCHILJH”, "EL4": "A:B::1243", “EL5”:“ADFD” },{ “EL1”:“”, "EL2": "ASDGFDSFFE 1:15", “市场”:“加州”, “EL3”:“CHCHILJH”, "EL4": "A:B::1243", “EL5”:“ADFD” }]

_.uniq(_.map(arr, 'MARKET')).sort()

这对我有用。

        $scope.nestAssociation = (arr) => {

            let newArr = JSON.parse(arr);

            const uniqueMarketNames = $scope._.uniqBy(newArr.message, 'MARKET'); // removed if had duplicate id

            let newMarketArray = [];
            for(var i = 0; i < uniqueMarketNames.length; i++) {
                newMarketArray.push(uniqueMarketNames[i].MARKET);
            }

            console.log("MARKETS: ", newMarketArray);

            return newMarketArray;
        }