使用两个条件

时间:2016-08-25 11:03:27

标签: javascript arrays

我一直在解决这个问题一段时间,没有解决方案。关于这个问题,我提出了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
}
]

4 个答案:

答案 0 :(得分:1)

根据我的理解,您希望有一个返回的数组:

  • 有2个或更多相同sameid并且
  • 的项目
  • 具有相同sameid但不包含parentid
  • 的任何其他项目

请尝试 - 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);

如果我弄错了,请告诉我

答案 1 :(得分:1)

您似乎希望识别出现多个sameidparentid值的sameid值,并且仅列出具有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);个值的对象。

这个ES6功能可以做到:

<body>
<form id="form1" runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
    <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="UploadMultipleFiles" />
    <hr />
    <asp:Label ID="Information" runat="server" ForeColor="Green" />
</form>
</body>

答案 2 :(得分:0)

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

除非您希望过滤器在其他情况下有所不同,否则会使其成为一个全新的问题

答案 3 :(得分: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));