min2bro
min2bro

Reputation: 4638

filter nested json in javascript

I have a JSON (My JSON) in which I want to filter out all the records with myRank equal to null. I am using array filter but not sure why it's not giving the desired output, instead I am getting a blank array. I want a new independent result. Don't want to alter the original data.

My JSON:

 ar = {
  "test": [
    {
      "id": "abc-123-def",
      "myResults": [
        {
          "distancetoriver": 2308.30,
          "profId": "klm-rtu-0987",
          "marketId": "51534",          
          "myRank": 1        
        },
        {
          "distancetoriver": 5612.29,
          "profId": "abc-def-0987",
          "marketId": "51535",          
          "myRank": null
        }        
      ]
    },
    {
      "id": "pqr-053-klm",
      "myResults": [
        {
          "distancetoriver": 1978.61,
          "profId": "sdc-poo-0968",
          "marketId": "51536",          
          "myRank": 2, 
        },
        {
          "distancetoriver": 15687.25,
          "profId": "foo-soo-0945",
          "marketId": "51539",          
          "myRank": null, 
        }
    ]
  },
  {
      "id": "qwe-053-qrt",
      "myResults": [
        {
          "distancetoriver": 3125.59,
          "profId": "red-ikj-0968",
          "marketId": "51542",          
          "myRank": null, 
        },
        {
          "distancetoriver": 5489.68,
          "profId": "frt-hyu-0945",
          "marketId": "51598",          
          "myRank": null, 
        }
    ]
  }
]
}

Desired Output:

{
  "test": [
    {
      "id": "abc-123-def",
      "myResults": [
        {
          "distancetoriver": 2308.30,
          "profId": "klm-rtu-0987",
          "marketId": "51534",          
          "myRank": 1        
        }   
      ]
    },
    {
      "id": "pqr-053-klm",
      "myResults": [
        {
          "distancetoriver": 1978.61,
          "profId": "sdc-poo-0968",
          "marketId": "51536",          
          "myRank": 2, 
        }
    ]
  },
  {
      "id": "qwe-053-qrt",
      "myResults": [

    ]
  }
]
}

My Code so far:

x = ar['test'].filter(function (obj) {
    obj.myResults.filter(function (item) {
        console.log(item.myRank)
        return item.myRank != null 
    });  
});

console.log(x)

Upvotes: 1

Views: 619

Answers (4)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

ar = {
  "test": [
    {
      "id": "abc-123-def",
      "myResults": [
        {
          "distancetoriver": 2308.30,
          "profId": "klm-rtu-0987",
          "marketId": "51534",          
          "myRank": 1        
        },
        {
          "distancetoriver": 5612.29,
          "profId": "abc-def-0987",
          "marketId": "51535",          
          "myRank": null
        }        
      ]
    },
    {
      "id": "pqr-053-klm",
      "myResults": [
        {
          "distancetoriver": 1978.61,
          "profId": "sdc-poo-0968",
          "marketId": "51536",          
          "myRank": 2, 
        },
        {
          "distancetoriver": 15687.25,
          "profId": "foo-soo-0945",
          "marketId": "51539",          
          "myRank": null, 
        }
    ]
  },
  {
      "id": "qwe-053-qrt",
      "myResults": [
        {
          "distancetoriver": 3125.59,
          "profId": "red-ikj-0968",
          "marketId": "51542",          
          "myRank": null, 
        },
        {
          "distancetoriver": 5489.68,
          "profId": "frt-hyu-0945",
          "marketId": "51598",          
          "myRank": null, 
        }
    ]
  }
]
}


 let result = {
      test: ar.test.reduce((acc, c) => {
            acc.push({ id: c.id,  
            myResults: c.myResults.filter(o => o.myRank)})
            return acc;
        }, [])
    };
    console.log(result);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386868

You could get new objects after filtering.

var data = { test: [{ id: "abc-123-def", myResults: [{ distancetoriver: 2308.3, profId: "klm-rtu-0987", marketId: "51534", myRank: 1 }, { distancetoriver: 5612.29, profId: "abc-def-0987", marketId: "51535", myRank: null }] }, { id: "pqr-053-klm", myResults: [{ distancetoriver: 1978.61, profId: "sdc-poo-0968", marketId: "51536", myRank: 2 }, { distancetoriver: 15687.25, profId: "foo-soo-0945", marketId: "51539", myRank: null }] }, { id: "qwe-053-qrt", myResults: [{ distancetoriver: 3125.59, profId: "red-ikj-0968", marketId: "51542", myRank: null }, { distancetoriver: 5489.68, profId: "frt-hyu-0945", marketId: "51598", myRank: null }] }] },
    result = {
        test: data.test.map(o => ({
            ...o,
            myResults: o.myResults.filter(({ myRank }) => myRank !== null)
        }))
    };

console.log(result);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 3

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92735

Try

let out = { 
  test: ar.test.map(x=> ({...x, myResults: x.myResults.filter(y=> y.myRank!==null)})) 
}

ar = {
  "test": [{
      "id": "abc-123-def",
      "myResults": [{
          "distancetoriver": 2308.30,
          "profId": "klm-rtu-0987",
          "marketId": "51534",
          "myRank": 1
        },
        {
          "distancetoriver": 5612.29,
          "profId": "abc-def-0987",
          "marketId": "51535",
          "myRank": null
        }
      ]
    },
    {
      "id": "pqr-053-klm",
      "myResults": [{
          "distancetoriver": 1978.61,
          "profId": "sdc-poo-0968",
          "marketId": "51536",
          "myRank": 2,
        },
        {
          "distancetoriver": 15687.25,
          "profId": "foo-soo-0945",
          "marketId": "51539",
          "myRank": null,
        }
      ]
    },
    {
      "id": "qwe-053-qrt",
      "myResults": [{
          "distancetoriver": 3125.59,
          "profId": "red-ikj-0968",
          "marketId": "51542",
          "myRank": null,
        },
        {
          "distancetoriver": 5489.68,
          "profId": "frt-hyu-0945",
          "marketId": "51598",
          "myRank": null,
        }
      ]
    }
  ]
}

let out = { 
  test: ar.test.map(x=> ({...x, myResults: x.myResults.filter(y=> y.myRank!==null)})) 
}

console.log(out);

Upvotes: 3

Maxime Girou
Maxime Girou

Reputation: 1570

As your "test" array has no need to be filtered, you can use "map"to iterate over it and return an array, the second filter seem's fine

x = ar['test'].map(function (obj) {
    obj.myResults.filter(function (item) {
        return item.myRank != null 
    }); 
    return obj;
});

Upvotes: 2

Related Questions