Andurit Andurit - 6 months ago 8
Javascript Question

Find object in JSON by knowing his id

I have an array of objects, where each object have subobjects with name

linkedParts
(please see example below), how can I find just that subobject based on subobject ID?

I already used something similar to find object by object ID and code looks like:

var updatedPart = axCalculation.selectedParts.filter(function( obj ) {
return obj.id === response.data.id;
})[0];


However I am not sure if I can search just knowing subobjects. If yes how?
axCalculation.selectedParts looksLike:

[{
"id": "1fccb481-53a0-400e-8831-80acd1793bee",
"euroCode": "7812BGSHABW",
"position": "REAR",
"category": "REAR_WINDOW",
"glassPartType": "GLASS",
"description": "TODO: DECODE 7812BGSHABW",
"calculatedPrice": 15768.00,
"operation": "REPLACE",
"selectionSource": "MANUAL",
"linkedParts": []
}, {
"id": "71e1dc3b-47a2-4406-970a-ef0911c93396",
"euroCode": "7812AGSHMVZ",
"position": "FRONT",
"category": "FRONT_WINDOW",
"glassPartType": "GLASS",
"description": "TODO: DECODE 7812AGSHMVZ",
"descriptionByUser": "TEST DESCC",
"calculatedPrice": 5749.50,
"priceByUser": 574.22,
"operation": "REPLACE",
"selectionSource": "MANUAL",
"linkedParts": [{
"id": "0f45cd6b-7053-4625-8ec9-87281c126e1d",
"partNumber": "LSD",
"description": "LEPÍCÍ SADA",
"calculatedPrice": 562.50,
"selectionSource": "AUTOMATIC",
"enabled": true
}, {
"id": "61bbbdad-1838-4327-8c38-8808a35a403c",
"partNumber": "string",
"description": "GEL POD SENZOR",
"calculatedPrice": 87.48,
"selectionSource": "AUTOMATIC",
"enabled": true
}]
}]

Answer

The only answer I saw which return effectively the linkedPart object is the reduce/filter one, but when I change the desired id for an invalid one, I still get something. So here the version I would use.

EDIT : I added a proof that changing the found object is changing the initial one

var selectedParts = [{
    "id": "1fccb481-53a0-400e-8831-80acd1793bee",
    "euroCode": "7812BGSHABW",
    "position": "REAR",
    "category": "REAR_WINDOW",
    "glassPartType": "GLASS",
    "description": "TODO: DECODE 7812BGSHABW",
    "calculatedPrice": 15768.00,
    "operation": "REPLACE",
    "selectionSource": "MANUAL",
    "linkedParts": []
}, {
    "id": "71e1dc3b-47a2-4406-970a-ef0911c93396",
    "euroCode": "7812AGSHMVZ",
    "position": "FRONT",
    "category": "FRONT_WINDOW",
    "glassPartType": "GLASS",
    "description": "TODO: DECODE 7812AGSHMVZ",
    "descriptionByUser": "TEST DESCC",
    "calculatedPrice": 5749.50,
    "priceByUser": 574.22,
    "operation": "REPLACE",
    "selectionSource": "MANUAL",
    "linkedParts": [{
        "id": "0f45cd6b-7053-4625-8ec9-87281c126e1d",
        "partNumber": "LSD",
        "description": "LEPÍCÍ SADA",
        "calculatedPrice": 562.50,
        "selectionSource": "AUTOMATIC",
        "enabled": true
    }, {
        "id": "61bbbdad-1838-4327-8c38-8808a35a403c",
        "partNumber": "string",
        "description": "GEL POD SENZOR",
        "calculatedPrice": 87.48,
        "selectionSource": "AUTOMATIC",
        "enabled": true
    }]
}];


//Search for the desired object(s)
var wantedId = "0f45cd6b-7053-4625-8ec9-87281c126e1d";
var linkedParts = [];
selectedParts.forEach(function(obj) {
  var foundLinkedParts = obj.linkedParts.filter(function(subObj) {
    return subObj.id == wantedId;
  });
  linkedParts = linkedParts.concat(foundLinkedParts);
});

console.log(linkedParts);

//Modify something in found object
linkedParts[0].partNumber = "My part number";
console.log(linkedParts[0].partNumber);

//Verify in object where initially stored
console.log(selectedParts[1]["linkedParts"][0].partNumber);

Comments