0

I have a specific format for a set of JSON objects. The format is as follows:

[{
    "key": ["key1"],
    "value": ["value1", "value2", "value3"]
}, {
    "key": ["key2", "key3"],
    "value": ["value4", "value5", "value6"]
}]

I am writing a function using simply JavaScript (no jQuery), that will append a value to the .value element based on if the user input matches a key value. For example, I input key2, the logic will match against key 2, and append "value7" to the end of the value element for that key, resulting in the following:

[{
    "key": ["key1"],
    "value": ["value1", "value2", "value3"]
}, {
    "key": ["key2", "key3"],
    "value": ["value4", "value5", "value6", "value7"]
}]

Currently the JSON is just an object in the JS file that is parsed using JSON.parse("string"). I would need to perform the appending and then rebuild the JSON using Stringify. (Assuming that would be my logic). I just need help with the appending because I am confused on the logic in this scenario. If anyone could throw together a quick example of how this would look in JS, that would be a massive help. Thank you!

0

3 Answers 3

2

You't target the object, and then the property containing the array, and push to that array

var array = [{
    "key": ["key1"],
    "value": ["value1", "value2", "value3"]
}, {
    "key": ["key2", "key3"],
    "value": ["value4", "value5", "value6"]
}];

array[1].value.push('value7');

console.log(array);

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

3 Comments

Ok well for this scenario, I have a few elements. I have the userInput that I run through JSON.parse to get the 2 elements (key, value). How do I determine where to push the value? I would need to run a (for i in array) and then set up a series of keys and then a (for j in keys) to set up the phrases arrays individually. I need to be able to match a userInput to the key so I know where to push the value to...if that makes sense.
Ok I think I have it working now. The strange thing is that even though I am doing a console.log output before and after the push and can see the the values are appended...the actual array doesn't change in the code. Why is that? Shouldn't that write to the file or is it something that is dynamic and isn't a permanent change?
It doesn't change the file, you have to write it back
0

check this snippet

var arr = [{
  "key": ["key1"],
  "value": ["value1", "value2", "value3"]
}, {
  "key": ["key2", "key3"],
  "value": ["value4", "value5", "value6"]
}]
console.log(insertAtKey(arr, 2, "value7"));

function insertAtKey(arr, index, str) {
  var obj = arr[index - 1];
  Object.keys(obj).forEach(function(key, val) {
    if (key === "value") {
      obj[key].push(str);
    }
  });
  arr[index - 1] = obj;
  return arr;
}

Hope it helps

Comments

0

This question sounds like homework to me and I don't want to spoil the whole solution, but you can use a filter for searching in the object array, for adding the value you have the rest of the puzzle:

var data = [{
    "key": ["key1"],
    "value": ["value1", "value2", "value3"]
}, {
    "key": ["key2", "key3"],
    "value": ["value4", "value5", "value6"]
}];

function hasKey(k) {
  return data.filter(e => { return e['key'].includes(k); });
}

console.log("for key1:" + hasKey("key1")[0].value);
console.log("for key2:" + hasKey("key2")[0].value);
console.log("for key3:" + hasKey("key3")[0].value);

What filter does:

  • e => { return e['key'].includes(k); } If the current object e in the data array includes a value k in the key attribute then pass the value.

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.