Lodash для фильтрации массива объектов - PullRequest
0 голосов
/ 26 сентября 2019

Я пытаюсь отфильтровать вложенный массив объектов, используя lodash, что довольно просто, но я стараюсь избегать многократных вызовов.

Я хочу создать 2 массива объектов, используя один вызов lodash /функция.Чтобы найти свойство объекта "$ isMultiAccount", если оно существует, поместите весь объект в один набор результатов, а если не поместите его в другой набор правил.

В настоящее время я делаю это с помощью Lodash "has and filter" для первого идля другого "! has" это означает, что один и тот же объект зацикливается дважды, так как объект относительно велик, его узкое место создает скорость

https://repl.it/repls/HomelyExpensiveTruetype

const item = {
  "domains": [
    {
      "id": "dm11022",
      "information":{
        "description": "Customer",
        "owner": {
          "primary":{
            "name": "James",
            "phone": "NA"
          },
          "others": [
            {
              "$isMultiAccount": "./Yes"
            }
          ]
        }
      }
    },
    {
      "id": "dm12022",
      "information":{
        "description": "Customer",
        "owner": {
          "primary":{
            "name": "James",
            "phone": "NA"
          },
          "others": [
            {
              "$isMultiAccount": "./No"
            }
          ]
        }
      }
    },
    {
      "id": "dm12022",
      "information":{
        "description": "Customer",
        "owner": {
          "primary":{
            "name": "James",
            "phone": "NA"
          },
          "others": [
            {
              "conf": {
                  "isVpnBased":{
                    "accountType": "Primary"
                  }
              }
            }
          ]
        }
      }
    }


  ]
}
/*
Expected result
  output1 = [
        {
      "id": "dm11022",
      "information":{
        "description": "Customer",
        "owner": {
          "primary":{
            "name": "James",
            "phone": "NA"
          },
          "others": [
            {
              "$isMultiAccount": "./Yes"
            }
          ]
        }
      }
    },
    {
      "id": "dm12022",
      "information":{
        "description": "Customer",
        "owner": {
          "primary":{
            "name": "James",
            "phone": "NA"
          },
          "others": [
            {
              "$isMultiAccount": "./No"
            }
          ]
        }
      }
    }
  ]

// $isMultiAccount account do not exist in this object
 output2 = [
       {
      "id": "dm12022",
      "information":{
        "description": "Customer",
        "owner": {
          "primary":{
            "name": "James",
            "phone": "NA"
          },
          "others": [
            {
              "conf": {
                  "isVpnBased":{
                    "accountType": "Primary"
                  }
              }
            }
          ]
        }
      }
    }
 ]


 */

1 Ответ

0 голосов
/ 29 сентября 2019
const item = {
"domains": [
{
  "id": "dm11022",
  "information":{
    "description": "Customer",
    "owner": {
      "primary":{
        "name": "James",
        "phone": "NA"
      },
      "others": [
        {
          "$isMultiAccount": "./Yes"
        }
      ]
    }
  }
},
{
  "id": "dm12022",
  "information":{
    "description": "Customer",
    "owner": {
      "primary":{
        "name": "James",
        "phone": "NA"
      },
      "others": [
        {
          "$isMultiAccount": "./No"
        }
      ]
    }
  }
},
{
  "id": "dm12022",
  "information":{
    "description": "Customer",
    "owner": {
      "primary":{
        "name": "James",
        "phone": "NA"
      },
      "others": [
        {
          "conf": {
              "isVpnBased":{
                "accountType": "Primary"
              }
          }
        }
      ]
    }
  }
}
]
}
const [areMulti, areNotMulti] = _.reduce(item.domains, (current, next) => {
  return _.has(next, ‘information.owner.others.$isMultiAccount’)
    ? [current[0].concat(next), current[1]]
    : [current[0], current[1].concat(next)];
}, [[], []]);
console.log(areMulti);
console.log(areNotMulti);
...