1

I would like to get filterd the particular array alone from the json response when dataID is not matched with the ParentDataID from another array in same json response using typescript feature in Angular 7

{ "data":[
    {
       "dataId":"Atlanta",
       "parentDataId":"America"
    },
    {
       "dataId":"Newyork",
       "parentDataId":"America"
    },
    {
       "dataId":"Georgia",
       "parentDataId":"Atlanta"
    },
    {
       "dataId":"South",
       "parentDataId":"Atlanta"
    },
    {
       "dataId":"North",
       "parentDataId":"South"
    }
   ]
}

In above response the value of dataId Newyork is not matched with any of the parentDataId entire array json response. So Now i want to filtered out only the second array of DataID alone to make new array.

I would like to have this validation in Typescript angular 7

My output is supposed to like below... The DataId does not have the parentDataId

[
  {
    "dataId":"Newyork",
    "parentDataId":"America"
  },
  {
     "dataId":"Georgia",
     "parentDataId":"Atlanta"
   },
   {
      "dataId":"North",
      "parentDataId":"South"
     }
]

Appreciate the help and response

  • Your question is not detailed enough to give you an exact solution. Still your are looking for the `Array.filter()` and `Array.some()` methods. Check out the documentation if you are not familiar with those. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – mamichels Apr 14 '20 at 08:18
  • 1
    in your sample data result output is dataId = Newyork, georgea, north are three records you want? – Developer Apr 14 '20 at 10:03
  • My intention is I want to get the arrays when dataId does not have parentId in whole JSON response. GaurangDhorda snippet is working fine but it doesn't look backward the response. GaurangDhorda can you help me pls? I appreciate your response. – user3175778 Apr 14 '20 at 17:12

3 Answers3

2

You can use filter method:

let filterKey = 'Atlanta';
const result = data.data.filter(f=> f.parentDataId != filterKey
   && f.dataId != filterKey);

An example:

let data = { "data":[
    {
       "dataId":"Atlanta",
       "parentDataId":"America"
    },
    {
       "dataId":"Newyork",
       "parentDataId":"America"
    },
    {
       "dataId":"Georgia",
       "parentDataId":"Atlanta"
    }
   ]
};

let filterKey = 'Atlanta';
const result = data.data.filter(f=> f.parentDataId != filterKey
    && f.dataId != filterKey);
console.log(result);
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Hi , Thanks for the response. I need exactly opposite. I just updated my question and can you please help me to get it. Thanks and Appreciate – user3175778 Apr 14 '20 at 08:32
  • @user3175778 Atlanta is already mapped into parentDataID so it shouldn't come in response. My reponse should be like beacuse the valueid of dattaId "Newyork" and "Georgia" is not present in any of the parentDataID value.. right. ? [ { "dataId":"Newyork", "parentDataId":"America" }, { "dataId": "Georgia", "parentDataId": "Atlanta" } ] – user3175778 Apr 14 '20 at 11:09
1

demo in this StackBlitz Link

my solution is like below code snippet. ts

reducedData = [...this.data];

this.data.reduce((c,n,i) => {
   this.data.reduce((d,o, inex) =>  { 
      if ( n.dataId === o.parentDataId){ 
           this.reducedData.splice(i,1, {'dataId': 'removed', parentDataId: 'true'}); 
      } else {
         return o;
      }
    },{});
   return n;
}, {});   

this.reducedData = this.reducedData.filter (value => value.dataId !== 'removed');

html file

<h4> dataId does not have parentId </h4>
<hr>
<pre>
  {{reducedData | json}}
</pre>

EDIT

If you do not want to use second object reducedData, then below solution is fine to work.. StackBlitz Link

component.ts

this.data.reduce((c,n,i) => {
    this.data.reduce((d,o, inex) =>  { 
      if ( n.dataId === o.parentDataId) {
      this.data[i]['removed'] = "removed";
      } else{
        return o;
      }
    },{});
   return n;
}, {});

this.data = this.data.filter (value => value['removed'] !== 'removed');

component.html

<h4> dataId does not have parentId </h4>
<hr>
<pre>
 {{data |json}}
</pre>
Developer
  • 3,309
  • 2
  • 19
  • 23
  • Hi Gaurang, Thanks for your response. Its working fine but getting failed when the dataId holds the value of parentDataID which comes first in the JSON response... In this scenario, it didn't reduce it and it skipped. when JSON response like below the issue is comes. { "dataId":"Georgia", "parentDataId":"Newyork" }, { "dataId":"Newyork", "parentDataId":"Georgia" }, ******* when ParentDataId value is "Newyork" comes first then following array of dataId calue is "Newyork" the code didnt reduced it. Can you please help me here... Appreciate – – user3175778 Apr 14 '20 at 17:48
  • @user3175778 i have updated my answer... checkout updated answer. – Developer Apr 14 '20 at 19:58
  • Hi Gaurang, Can I have an updated slackBlitz link for the fix? the provided one, I couldn't find it. Thanks. – user3175778 Apr 15 '20 at 05:19
  • @user3175778 https://stackblitz.com/edit/angular-gnhjqi?file=src%2Fapp%2Fapp.component.ts here is updated stackblitz link. as same link is updated in answer too. – Developer Apr 15 '20 at 06:31
  • @user3175778 please, `upvote answer`. Now you can upvote answer, because i have upvoted question and you have now required reputation for upvote. – Developer Apr 15 '20 at 06:35
0

Please try like this.

const data = { "data":[
    {
       "dataId":"Atlanta",
       "parentDataId":"America"
    },
    {
       "dataId":"Newyork",
       "parentDataId":"America"
    },
    {
       "dataId":"Georgia",
       "parentDataId":"Atlanta"
    }
   ]
};

const filterKey = "Newyork"
const matchExist = data.data.some( item => item.parentDataId ===  filterKey && item.dataId === filterKey)
let filteredArray ;
if(!matchExist){
    filteredArray = data.data.find(item => item.dataId === filterKey )
}
Sayooj V R
  • 2,255
  • 2
  • 11
  • 23