2016-08-25 3 views
-2

Я уже давно ломаю голову вокруг этой проблемы, без решения. Я задал 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 
} 
] 
+0

Я не понимаю, как генерируется выходной, может у меня есть какое-то объяснение? –

+1

Так вы просто копируете свой собственный вопрос из http://stackoverflow.com/questions/39140297/filtering-an-array-of-objects-several-criteria? Если да, есть ли причина, по которой вы не просто отредактировали вопрос для ясности? – moopet

+0

Мне было лучше задать новый вопрос, чтобы избежать путаницы. –

ответ

1

Похоже, вы хотите, чтобы определить sameid значения, которые происходят с более чем одной отдельной parentid значения, а только список объектов с теми sameid значениями.

Эта функция ES6 будет делать:

function getChildrenWithMultipleParents(data) { 
 
    // Create a Map keyed by sameid, each with Set of its combined parentids 
 
    var mp = data.reduce( 
 
     (mp, obj) => mp.set(obj.sameid, 
 
          (mp.get(obj.sameid) || new Set()).add(obj.parentid)), 
 
     new Map()); 
 
    // Return the objects that have sameid values with mulitple distinct parentids 
 
    return data.filter (obj => mp.get(obj.sameid).size > 1); 
 
} 
 

 
// Sample data 
 
var data2 = [{ name: "one", sameid: 100, parentid: 10 }, 
 
      { name: "two", sameid: 200, parentid: 62 }, 
 
      { name: "three", sameid: 200, parentid: 62 }, 
 
      { name: "four", sameid: 300, parentid: 72 }, 
 
      { name: "five", sameid: 300, parentid: 72 }, 
 
      { name: "six", sameid: 300, parentid: 73 }]; 
 

 
// Perform filter 
 
var result = getChildrenWithMultipleParents(data2); 
 

 
// Output result 
 
console.log(result);

0
desiredResult = data2.filter(e => e.sameid === '300'); 

Если вы не хотите, чтобы фильтр отличаться в других случаях, что сделало бы его совершенно новой проблемой

1

Как я понимаю, вы хотели бы иметь массив, возвращающие:

  • Есть 2 или более элементов с одинаковым sameid AndAlso
  • Любой другой пункт с той же sameid, но не parentid

Пожалуйста TRY - https://jsfiddle.net/bbfh8szj/

function GetDisiredArray(Arr) { 
    var DisiredArray = []; 
    for (var i = 0; i < Arr.length; i++) { 
    if (GetIsPotentialItem(Arr, i)) 
    DisiredArray[DisiredArray.length] = Arr[i]; 
    } 

    $("div").text(JSON.stringify(DisiredArray)); 
} 

function GetIsPotentialItem(Arr, Index) { 
    for (var i = 0; i < Arr.length; i++) { 
    if (i != Index && Arr[i].sameid == Arr[Index].sameid && Arr[i].parentid != Arr[Index].parentid) 
     return true; 
    } 
    return false; 
} 

GetDisiredArray(data2); 

Пожалуйста, дайте мне знать, если я ошибаюсь

0

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 sameIdBucket = {}; 
 
    data2.forEach(function(item){ 
 
     if(!sameIdBucket.hasOwnProperty(item.sameid)){ 
 
       sameIdBucket[item.sameid] = {uniqueParentIds: [], items: []}; 
 
     } 
 
    
 
     var curr = sameIdBucket[item.sameid]; 
 
    
 
     curr.items.push(item); 
 
     if(curr.uniqueParentIds.indexOf(item.parentid) === -1){ 
 
      curr.uniqueParentIds.push(item.parentid); 
 
     } 
 
    }); 
 
    
 
    var result = Object.keys(sameIdBucket).map(function(key){ return sameIdBucket[key] }).filter(function(item){ return item.items.length > 1 && item.uniqueParentIds.length > 1 }).map(function(item){ return item.items; }); 
 
    document.write(JSON.stringify(result));

Смежные вопросы