Search code examples
javascriptarraysobjectinsertcompare

Insert Object in Array before other object in array with specific property


I have one Array with objects. All objects habe one unique id. Now I want to insert a new Object before the object with the specific id. To do that I get this object:

{"topic":"add","payload":{"id":1,"end":"24:00","temp":21,"add":0,"delete":0},"row":-1,"socketid":"SSZY-1C3jiP8-NS2AAAB","_msgid":"a891f409.e30038"}

and now I need to find this object in this array with the id:

[{"woche":"Montag","status":0,"_children":[{"id":1,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Dienstag","status":0,"vortag":1,"_children":[{"id":2,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Mittwoch","start":"","status":0,"vortag":1,"_children":[{"id":3,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Donnerstag","status":0,"vortag":1,"_children":[{"id":4,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Freitag","start":"","status":0,"vortag":1,"_children":[{"id":5,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Samstag","start":"","status":0,"vortag":1,"_children":[{"id":6,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Sonntag","start":"","status":0,"vortag":1,"_children":[{"id":7,"end":"24:00","temp":21,"add":0,"delete":0}]}]

and now I want to insert a new object bofore the object with the matching id.

How can I find the position in the array and how can I add this object?

{"id":`${++Id}`,"end":"","temp":21,"add":0,"delete":0}

At the end it should look like this:

[{"woche":"Montag","status":0,"_children":[{"id":"8","end":"","temp":21,"add":0,"delete":0},{"id":1,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Dienstag","status":0,"vortag":1,"_children":[{"id":2,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Mittwoch","start":"","status":0,"vortag":1,"_children":[{"id":3,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Donnerstag","status":0,"vortag":1,"_children":[{"id":4,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Freitag","start":"","status":0,"vortag":1,"_children":[{"id":5,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Samstag","start":"","status":0,"vortag":1,"_children":[{"id":6,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Sonntag","start":"","status":0,"vortag":1,"_children":[{"id":7,"end":"24:00","temp":21,"add":0,"delete":0}]}]

Solution

  • You can use filter method to check for the index of the specific element.

    Suppose this is your array:

    let arr = [{"woche":"Montag","status":0,"_children":[{"id":1,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Dienstag","status":0,"vortag":1,"_children":[{"id":2,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Mittwoch","start":"","status":0,"vortag":1,"_children":[{"id":3,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Donnerstag","status":0,"vortag":1,"_children":[{"id":4,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Freitag","start":"","status":0,"vortag":1,"_children":[{"id":5,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Samstag","start":"","status":0,"vortag":1,"_children":[{"id":6,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Sonntag","start":"","status":0,"vortag":1,"_children":[{"id":7,"end":"24:00","temp":21,"add":0,"delete":0}]}]
    

    You need to search for the index of this object in the array:

    let s = {"topic":"add","payload":{"id":1,"end":"24:00","temp":21,"add":0,"delete":0},"row":-1,"socketid":"SSZY-1C3jiP8-NS2AAAB","_msgid":"a891f409.e30038"}
    

    You can use the following query to achieve this:

    let index = -1
    arr.filter((o) => {
        if(s.payload.id === o._children[0].id) {
            index = arr.indexOf(o)
        }
        return o
    })
    

    You can use splice method to add new object in the array at specific index.

    arr.splice(index, 0, <value to enter in array>)