0

I have js objects like below

{
    "1": {
        "id": 1,
        "ltp": 35110
    },
    "2": {
        "id": 2,
        "ltp": 35110
    },
    "3": {
        "id": 3,
        "ltp": 35110
    },
    "4": {
        "id": 4,
        "ltp": 35110
    },
    "5": {
        "id": 5,
        "ltp": 35109.35
    },
    "6": {
        "id": 6,
        "ltp": 35109.75
    },
    "7": {
        "id": 7,
        "ltp": 35105.3
    },
    
}

How do I run filter and remove values if property values are less than N . Without property names I can remove by

myArray = myArray.filter(function( obj ) {
        return obj.id > cleanSize;
    });

Here properties are string "1" , "2" .. etc.

Gracie williams
  • 1,287
  • 2
  • 16
  • 39

1 Answers1

0

const threshold = 4;
const input = {
    "1": {
        "id": 1,
        "ltp": 35110
    },
    "2": {
        "id": 2,
        "ltp": 35110
    },
    "3": {
        "id": 3,
        "ltp": 35110
    },
    "4": {
        "id": 4,
        "ltp": 35110
    },
    "5": {
        "id": 5,
        "ltp": 35109.35
    },
    "6": {
        "id": 6,
        "ltp": 35109.75
    },
    "7": {
        "id": 7,
        "ltp": 35105.3
    },
    
};

const output = {};
Object.keys(input).forEach((key) => {
  if (Number(input[key].id) >= threshold) {
    output[key] = input[key];
  }
});
console.log(output);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49