简体   繁体   English

将字符串追加到JS中的JSON数组元素

[英]Append string to JSON array element in JS

I have a specific format for a set of JSON objects. 我有一组JSON对象的特定格式。 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. 我正在使用简单的JavaScript(没有jQuery)编写一个函数,该函数将根据用户输入是否匹配键值将一个值附加到.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: 例如,我输入key2,逻辑将与键2匹配,并将“ value7”附加到该键的value元素的末尾,结果如下:

[{
    "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"). 当前,JSON只是使用JSON.parse(“ string”)解析的JS文件中的对象。 I would need to perform the appending and then rebuild the JSON using Stringify. 我需要执行附加操作,然后使用Stringify重建JSON。 (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. 如果有人可以举一个简短的例子说明这在JS中的外观,那将是巨大的帮助。 Thank you! 谢谢!

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); 

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 希望能帮助到你

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); } 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. e => { return e['key'].includes(k); }如果data数组中的当前对象ekey属性中包含值k ,则传递该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM