0

I have the following Array of objects:

let objOperacion = [
        {
            "OPERACION": {
            "ID_OP": 1
            },
            "GEOJSON": [
                {
                    "type": "Feature",
                    "properties": {
                        "ID": 40,
                        "SUPERFICIE": 572.7
                    },
                }
            ]
        },
        {
            "OPERACION": {
            "ID_OP": 2
            },
            "GEOJSON": [
                {
                    "type": "Feature",
                    "properties": {
                        "ID": 41,
                        "SUPERFICIE": 572.7
                    },
                }
            ]
        },
        {
            "OPERACION": {
            "ID_OP": 1
            },
            "GEOJSON": [
                {
                    "type": "Feature",
                    "properties": {
                        "ID": 42,
                        "SUPERFICIE": 572.7
                    },
                }
            ]
        }
]

I have created a function that when passing an id as parameter, it takes all the objects that have the same id.

I have done it in the following way:

for (var i = 0; i < objOperacion.length; i++) {
    if (id === objOperacion[i].feature.properties.ID) {
        objOperacion[i].setStyle(styleWorking);
        console.log("SAME ID");
                
    } else {
        objOperacion[i].setStyle(style);
        console.log("DIFFERENT");
    }
}

What I am trying to do is to create another function to catch objects that have the same "ID_OP". But I can't get it.

1

4 Answers 4

1

Like GrafiCode suggested, it seems the best solution is to use Array.filter()

So, code would look like something like this:

let objWithSameOp_Ids = objOperacion.filter(op=>{
if(id === op.OP_ID){
return op}
})

That way you get all objects with same OP_IDs.

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

Comments

0

Try this

for (var i = 0; i < objOperacion.length; i++) {
    if (id === objOperacion[i].OPERACION.ID_OP) {
        objOperacion[i].setStyle(styleWorking);
        console.log("SAME ID");
                
    } else {
        objOperacion[i].setStyle(style);
        console.log("DIFFERENT");
    }
}

Comments

0

We can using Array.reduce() to find the id which appear more than once,then using Array.filter() to get the expected result

let ids = Object.entries(data.reduce((a,v) => {
  a[v.OPERACION.ID_OP] = a[v.OPERACION.ID_OP]??0
  a[v.OPERACION.ID_OP] += 1
  return a
},{})).filter(d => d[1] > 1).map(d => +d[0])

let result = data.filter(d => ids.includes(d.OPERACION.ID_OP))
console.log(result)

let data = [
        {
            "OPERACION": {
            "ID_OP": 1
            },
            "GEOJSON": [
                {
                    "type": "Feature",
                    "properties": {
                        "ID": 40,
                        "SUPERFICIE": 572.7
                    },
                }
            ]
        },
        {
            "OPERACION": {
            "ID_OP": 2
            },
            "GEOJSON": [
                {
                    "type": "Feature",
                    "properties": {
                        "ID": 41,
                        "SUPERFICIE": 572.7
                    },
                }
            ]
        },
        {
            "OPERACION": {
            "ID_OP": 1
            },
            "GEOJSON": [
                {
                    "type": "Feature",
                    "properties": {
                        "ID": 42,
                        "SUPERFICIE": 572.7
                    },
                }
            ]
        }
]

let ids = Object.entries(data.reduce((a,v) => {
  a[v.OPERACION.ID_OP] = a[v.OPERACION.ID_OP]??0
  a[v.OPERACION.ID_OP] += 1
  return a
},{})).filter(d => d[1] > 1).map(d => +d[0])

let result = data.filter(d => ids.includes(d.OPERACION.ID_OP))
console.log(result)

Comments

0
function getID_OP(id) {
    objOperacion.forEach(item => {
        if (item['OPERACION']['ID_OP'] === id) console.log('SAME ID')
    })
}

You will have to iterate over the objects to change them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.