筛选出同一键为空(“”)上的对象数组

时间:2018-10-08 14:52:43

标签: javascript arrays json

我有一个需要过滤的对象数组。看起来像这样:

        let array = [
            {
                "id": "",
                "first_name": "Kary",
                "last_name": "Thorndale",
                "email": "kthorndale1@nifty.com",
                "gender": "Female",
                "ip_address": "172.152.36.109"
            },
            {
                "id": "",
                "first_name": "Westley",
                "last_name": "Emmott",
                "email": "wemmott2@cisco.com",
                "gender": "Male",
                "ip_address": "104.62.125.170"
            },
            {
                "id": "",
                "first_name": "Gavrielle",
                "last_name": "Danihel",
                "email": "gdanihel3@yandex.ru",
                "gender": "Female",
                "ip_address": "98.98.209.17"
            }
    ];

我只有一个条件-如果所有对象中的键为空,则将其从所有对象中删除。

如果速度更快或有需要,我可以使用jQuery或loDash。

数组中的对象不应超过15-20个,最多需要有15个这样的数组需要通过过滤器。

3 个答案:

答案 0 :(得分:1)

您可以计算相同键的空值并映射没有所有emty属性的新对象。

var array = [{ id: "", first_name: "Kary", last_name: "Thorndale", email: "kthorndale1@nifty.com", gender: "Female", ip_address: "172.152.36.109" }, { id: "", first_name: "Westley", last_name: "Emmott", email: "wemmott2@cisco.com", gender: "Male", ip_address: "104.62.125.170" }, { id: "", first_name: "Gavrielle", last_name: "Danihel", email: "gdanihel3@yandex.ru", gender: "Female", ip_address: "98.98.209.17" }],
    keys = Array
        .from(array.reduce((m, o) => {
            Object.entries(o).forEach(([k, v]) => m.set(k, (m.get(k) || 0) + +!!v));
            return m;
        }, new Map))
        .filter(({ 1: v }) => v)
        .map(([k]) => k),
    result = array.map(o => Object.assign(...keys.map(k => ({ [k]: o[k] }))));

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

带有Set

的更好的版本

var array = [{ id: "", first_name: "Kary", last_name: "Thorndale", email: "kthorndale1@nifty.com", gender: "Female", ip_address: "172.152.36.109" }, { id: "", first_name: "Westley", last_name: "Emmott", email: "wemmott2@cisco.com", gender: "Male", ip_address: "104.62.125.170" }, { id: "", first_name: "Gavrielle", last_name: "Danihel", email: "gdanihel3@yandex.ru", gender: "Female", ip_address: "98.98.209.17" }],
    keys = Array.from(
        array.reduce(
            (s, o) => Object.entries(o).reduce((t, [k, v]) => v ? t.add(k) : t, s),
            new Set
        )
    ),
    result = array.map(o => Object.assign(...keys.map(k => ({ [k]: o[k] }))));

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

答案 1 :(得分:0)

您可以使用findIndex来检查是否存在空字符串作为该特定键的值。如果它是-1,则该键没有空字符串作为值。

如果它不大于-1,则可以使用map返回一个新数组,并在回调函数中创建一个新对象并设置键及其值

let array = [{
    "id": "",
    "first_name": "Kary",
    "last_name": "Thorndale",
    "email": "kthorndale1@nifty.com",
    "gender": "Female",
    "ip_address": "172.152.36.109"
  },
  {
    "id": "",
    "first_name": "Westley",
    "last_name": "Emmott",
    "email": "wemmott2@cisco.com",
    "gender": "Male",
    "ip_address": "104.62.125.170"
  },
  {
    "id": "",
    "first_name": "Gavrielle",
    "last_name": "Danihel",
    "email": "gdanihel3@yandex.ru",
    "gender": "Female",
    "ip_address": "98.98.209.17"
  }
];

let getIndex = array.findIndex(function(item) {
  return item.id === '';

})
let newArray = [];
if (getIndex !== -1) {
  newArray = array.map(function(item) {
    return Object.assign({}, {
      "first_name": item['first_name'],
      "last_name": item['last_name'],
      "email": item['email'],
      "gender": item['gender'],
      "ip_address": item['ip_address'],
      "item.gender": item['item.gender']
    })
  })
}

console.log(newArray)

或者,您可以使用数组somereduce

let array = [{
    "id": "",
    "first_name": "Kary",
    "last_name": "Thorndale",
    "email": "kthorndale1@nifty.com",
    "gender": "Female",
    "ip_address": "172.152.36.109"
  },
  {
    "id": "",
    "first_name": "Westley",
    "last_name": "Emmott",
    "email": "wemmott2@cisco.com",
    "gender": "Male",
    "ip_address": "104.62.125.170"
  },
  {
    "id": "",
    "first_name": "Gavrielle",
    "last_name": "Danihel",
    "email": "gdanihel3@yandex.ru",
    "gender": "Female",
    "ip_address": "98.98.209.17"
  }
];

let __e = array.some((item) => {
  return item.id === '';
})
if (__e) {
  let z = array.reduce(function(acc, curr) {
    let k = Object.assign({}, curr);
    delete k['id'];
    acc.push(k);
    return acc

  }, [])
  console.log(z)
}

答案 2 :(得分:0)

这是lodash的简洁解决方案。

let data = [ { "id": "", "first_name": "Kary", "last_name": "Thorndale", "email": "kthorndale1@nifty.com", "gender": "Female", "ip_address": "172.152.36.109" }, { "id": "", "first_name": "Westley", "last_name": "Emmott", "email": "wemmott2@cisco.com", "gender": "Male", "ip_address": "104.62.125.170" }, { "id": "", "first_name": "Gavrielle", "last_name": "Danihel", "email": "gdanihel3@yandex.ru", "gender": "Female", "ip_address": "98.98.209.17" } ];

const mrg = (r,c) => _.mergeWith(r,c, (o,s) => _.isEmpty(o) ? s : o)

const result = _.map(data, x => 
  _.omit(x,_.chain(data).reduce(mrg).pickBy(_.isEmpty).keys().value()))

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

这里的主要思想是将所有对象合并为一个,因为那样一来,我们将立即知道到处哪个键为空。我们使用mrg函数执行此操作,该函数内部使用mergeWith提供customizer。该功能的主要目的是确保我们合并一个非空值。