-1

I was given an existing project where the data structure is below:

[
    {
        "key": "username",
        "value": ""
    },
    {
        "key": "password",
        "value": ""
    },
    {
        "key": "cars",
        "value": [
            {"ABC-1234-1234": "s4LmoNzee9Xr6f7uu/"},
            {"ABC-5678-5678": "s5LmoNzee9Xr5f9uu/"}
        ]
    }
]

Cars' value is an array of objects.

To create the initial object in the cars' array, I do the following:

    var encryptedStuff = json.data;
    var carkey = carid;
    var entry = {};
    entry[carkey] = encryptedStuff;
    var carArray = [];
    
    key="cars";

    carArray.push(entry);

I need to push the array into another function that turns it into a string in a stored variable.

My problem...and I'm really rusty on embedded objects...is to do the following:

1.) Get the string 2.) JSON.parse back into an object (I got this far as I'm using a jQuery grep but I'd prefer to use JavaScript).

Here's my problem... 3.) locate the cars key in the object and get its value. 4.) Turn the value into an array to either delete an item or to add one (as per the code above where I'm writing the object into the array.

In the case of adding, I would have to copy the cars' value into the carArray[] and then push the new item into it.

In the case of deleting, I would have to remove the item and push back everything back into the carArray[].

I would do things differently but I can't change the structure of the data as this is approved company-wide.

Any help would be appreciated.

Thanks

user581733
  • 484
  • 3
  • 9
  • 18

1 Answers1

1

You don't need to make a copy of the car's value array to add new entries nor make a copy then "push back" to remove an entry - you can reference it directly in the parsed object.

You can use

Giving:

var source  = `[
    {
        "key": "username",
        "value": ""
    },
    {
        "key": "password",
        "value": ""
    },
    {
        "key": "cars",
        "value": [
            {"ABC-1234-1234": "s4LmoNzee9Xr6f7uu/"},
            {"ABC-5678-5678": "s5LmoNzee9Xr5f9uu/"}
        ]
    }
]`

// convert json to an object
var data = JSON.parse(source);
console.log(data, data.find(e=>e.key=="cars").value)

// add an item to the cars.value
data.find(e=>e.key=="cars").value.push({"ABC-": "s6..." });

// remove an item from the cars.value
data.find(e=>e.key=="cars").value.splice(1,1);

// confirm items added/removed
console.log(data, data.find(e=>e.key=="cars").value)

// convert back to a strng to send back to the service
var result = JSON.stringify(data);
console.log(result);
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • How would I use these? This is an array of objects. – user581733 Jul 10 '21 at 23:07
  • *how would I use these* - as shown in the answer's code – freedomn-m Jul 11 '21 at 12:08
  • I was hoping for an example. I looked up these items in the answer before I posted here. – user581733 Jul 11 '21 at 16:21
  • *I was hoping for an example* - you mean like the example in the answer? I'm entirely not sure what more you want? Can you see the code snippet above? Add a new item: `data.find(e=>e.key=="cars").value.push({"ABC-": "s6..." });` remove an item: `data.find(e=>e.key=="cars").value.splice(1,1);` – freedomn-m Jul 12 '21 at 07:55