1

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)
0

4 Answers 4

3

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);

Sign up to request clarification or add additional context in comments.

2 Comments

@min2bro - I add console.log(ar) - and I see that orgianl array is NOT altered - maybe I miss something - exact which field is changed?
try to run this code in fresh chrome console - it not alter ar (may be some other part of your js code change it?)
3

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; }

2 Comments

This changes the data variable, Do a console.log(data) and the data will be altered.
no, it spreads the object and adds a new subset of myResults. i see nomutation here.
2

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;
});

2 Comments

This will change my original data. I am looking for a new filtered data
Neither filter nor map will change your original data
1

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.