Я уже давно ломаю голову вокруг этой проблемы, без решения. Я задал SO вопрос относительно проблемы, и у меня появилось много ответов. К сожалению, ответы не подходят для моей проблемы. Наверное, потому, что я не задал свои требования должным образом. Спасибо всем за отличные ответы, я многому научился у них.Фильтр массива объекта с двумя критериями
Моя задача состоит в том, чтобы фильтровать массив объектов на основе двух значений (sameid и ParentId).
Задача:
var data2 = [
{ // Case one. Should not return it. There is only one result, with sameid=100 and parentid=10
name: "one",
sameid: 100,
parentid: 10
},
{//Case two: should not return any result, since there both sameid and parentid are the same
name: "two",
sameid: 200,
parentid: 62
},
{//Case two: should not return any result, since there both sameid and parentid are the same
name: "three",
sameid: 200,
parentid: 62
},
{//Case three. Should return all three results. Four and five have identical sameid and parentid. But six has different parentid
name: "four",
sameid: 300,
parentid: 72
},
{//Case three. Should return all three results. Four and five have identical sameid and parentid. But six has different parentid
name: "five",
sameid: 300,
parentid: 72
},
{//Case three. Should return all three results. Four and five have identical sameid and parentid. But six has different parentid
name: "six",
sameid: 300,
parentid: 73
}
]
После фильтрации делается, я должен иметь этот массив объектов:
var desiredResult = [
{//Case three. Should return all three results. Four and five have identical
sameid and parentid. But six has different parentid
name: "four",
sameid: 300,
parentid: 72
},
{//Case three. Should return all three results. Four and five have identical
sameid and parentid. But six has different parentid
name: "five",
sameid: 300,
parentid: 72
},
{//Case three. Should return all three results. Four and five have identical
sameid and parentid. But six has different parentid
name: "six",
sameid: 300,
parentid: 73
}
]
Я не понимаю, как генерируется выходной, может у меня есть какое-то объяснение? –
Так вы просто копируете свой собственный вопрос из http://stackoverflow.com/questions/39140297/filtering-an-array-of-objects-several-criteria? Если да, есть ли причина, по которой вы не просто отредактировали вопрос для ясности? – moopet
Мне было лучше задать новый вопрос, чтобы избежать путаницы. –