用于多维json对象的Javascript过滤器

时间:2022-08-30 14:01:24

Can't use javascript filter in multi-dimensional object.

不能在多维对象中使用javascript过滤器。

var object = [{
  "id": "1",
  "name": "General",
  "cards": [{
    "id": "1",
    "name": "shawn"
  }, {
    "id": "2",
    "name": "neo"
  }]
}, {
  "id": "2",
  "name": "CEO",
  "cards": [{
    "id": "1",
    "name": "Raman"
  }, {
    "id": "2",
    "name": "Sheena"
  }]
}]

function searchFor(item) {
  return item.cards.filter(
    (card) => {
      return card.name.indexOf("Raman") !== -1;
    }
  );
}

var filtered = object.filter(searchFor);

console.log(filtered);

This is how I am trying, inside the searchFor card.name I am getting the correct card name but filtering is returning all the cards.Its not filtering.

这就是我正在尝试的方式,在searchFor card.name中我得到了正确的卡名,但过滤正在返回所有的卡。它不会过滤。

Could any help me with this.

能帮助我吗?

2 个解决方案

#1


0  

You were returning the filtered array, which would produce a TRUE result whenever cards existed. So you can just turn that into a boolean, by saying when the item.cards.filter(...).length > 0.

您正在返回已过滤的数组,只要存在卡,就会产生TRUE结果。所以你可以把它变成一个布尔值,通过说当item.cards.filter(...)。length> 0。

var object = [{
  "id": "1",
  "name": "General",
  "cards": [{
    "id": "1",
    "name": "shawn"
  }, {
    "id": "2",
    "name": "neo"
  }]
}, {
  "id": "2",
  "name": "CEO",
  "cards": [{
    "id": "1",
    "name": "Raman"
  }, {
    "id": "2",
    "name": "Sheena"
  }]
}]

var searchFor = (card) => card.name.indexOf("Raman") > -1;
var filteredCards = object.reduce((cards, item) => cards.concat(item.cards.filter(searchFor)), []);
var filteredObj = object.map(i => {
  i.cards = i.cards.filter(searchFor);
  return i;
}).filter(i => i.cards.length)

console.log(filteredCards, filteredObj)

Updated

I updated the code snippet to produce either the cards which were found. I also provide a method for returning all objects which contain the needed cards, and filter out the other cards.

我更新了代码片段以生成找到的卡片。我还提供了一种方法,用于返回包含所需卡片的所有对象,并过滤掉其他卡片。

#2


1  

An empty array isn't considered falsey in Javascript. So instead of returning the result of filtering the cards array, test its length.

在Javascript中,空数组不被视为falsey。因此,不是返回过滤卡阵列的结果,而是测试其长度。

var object = [{
  "id": "1",
  "name": "General",
  "cards": [{
    "id": "1",
    "name": "shawn"
  }, {
    "id": "2",
    "name": "neo"
  }]
}, {
  "id": "2",
  "name": "CEO",
  "cards": [{
    "id": "1",
    "name": "Raman"
  }, {
    "id": "2",
    "name": "Sheena"
  }]
}]

function searchFor(item) {
  return item.cards.filter(
    (card) => {
      return card.name.indexOf("Raman") !== -1;
    }
  ).length != 0;
}

var filtered = object.filter(searchFor);

console.log(filtered);

#1


0  

You were returning the filtered array, which would produce a TRUE result whenever cards existed. So you can just turn that into a boolean, by saying when the item.cards.filter(...).length > 0.

您正在返回已过滤的数组,只要存在卡,就会产生TRUE结果。所以你可以把它变成一个布尔值,通过说当item.cards.filter(...)。length> 0。

var object = [{
  "id": "1",
  "name": "General",
  "cards": [{
    "id": "1",
    "name": "shawn"
  }, {
    "id": "2",
    "name": "neo"
  }]
}, {
  "id": "2",
  "name": "CEO",
  "cards": [{
    "id": "1",
    "name": "Raman"
  }, {
    "id": "2",
    "name": "Sheena"
  }]
}]

var searchFor = (card) => card.name.indexOf("Raman") > -1;
var filteredCards = object.reduce((cards, item) => cards.concat(item.cards.filter(searchFor)), []);
var filteredObj = object.map(i => {
  i.cards = i.cards.filter(searchFor);
  return i;
}).filter(i => i.cards.length)

console.log(filteredCards, filteredObj)

Updated

I updated the code snippet to produce either the cards which were found. I also provide a method for returning all objects which contain the needed cards, and filter out the other cards.

我更新了代码片段以生成找到的卡片。我还提供了一种方法,用于返回包含所需卡片的所有对象,并过滤掉其他卡片。

#2


1  

An empty array isn't considered falsey in Javascript. So instead of returning the result of filtering the cards array, test its length.

在Javascript中,空数组不被视为falsey。因此,不是返回过滤卡阵列的结果,而是测试其长度。

var object = [{
  "id": "1",
  "name": "General",
  "cards": [{
    "id": "1",
    "name": "shawn"
  }, {
    "id": "2",
    "name": "neo"
  }]
}, {
  "id": "2",
  "name": "CEO",
  "cards": [{
    "id": "1",
    "name": "Raman"
  }, {
    "id": "2",
    "name": "Sheena"
  }]
}]

function searchFor(item) {
  return item.cards.filter(
    (card) => {
      return card.name.indexOf("Raman") !== -1;
    }
  ).length != 0;
}

var filtered = object.filter(searchFor);

console.log(filtered);