[英]create custom nested output from json using javascript
let data = { "needData": [ { arrayData: [ { "key": "dummy", "value": "needed value" }, { "key": "secret", "random": "secret_random" }, { "key": "another", "value": "testing" }, { "key": "another_Secret", "random": "testing_value" }, ] } ] }; let json, testing; data.needData.map((value) => { json = { arrayData: [ { needed_key: value.arrayData[0].key, needed_value: value.arrayData[0].value, }, ], }; testing = { arrayData: [ { needed_key: value.arrayData.key, needed_value: value.arrayData.value, }, ], }; }); console.log("value got by running json", json); console.log("value got by running testing", testing);
I am trying to save data in json object by using map function but the problem is I can save data by using needed_key:value.arrayData.key
needed_value:value.arrayData.value
我正在尝试使用 json object 保存数据 map function 但问题是我可以使用needed_key:value.arrayData.key
needed_value:value.arrayData.value
保存数据
if all the needed_key and needed_value are same I can use above code but here problem is key name is same but the needed_value get change by random and value
I only want to save values of those data which contain in value
not random
is there any way I can get key and value
value not key and random
value如果所有 needed_key 和 needed_value 都相同,我可以使用上面的代码,但这里的问题是键名相同,但 needed_value random and value
我只想保存那些包含在值中的数据的value
不是random
的有什么办法我可以得到key and value
值不是key and random
值
I can use manual code to fetch the data but what if data is more and I don't want to use manual code我可以使用手动代码来获取数据,但是如果数据更多并且我不想使用手动代码怎么办
{
"arrayData": [
{
"needed_key": "dummy",
"needed_value": "needed value"
},
{
"needed_key": "another",
"needed_value": "testing"
}
]
}
This gives your output:这给你的 output:
var output = {
'arrayData': data.needData[0].arrayData
.filter(x => !Object.keys(x).includes('random'))
.map(x => {
return {needed_key: x['key'], needed_value: x['value']}
})
}
console.log(output);
Array.map
and Array.filter
implementation. Array.map
和Array.filter
实现。
let data = { "needData": [ { arrayData: [ { "key": "dummy", "value": "needed value" }, { "key": "secret", "random": "secret_random" }, { "key": "another", "value": "testing" }, { "key": "another_Secret", "random": "testing_value" }, ] } ] }; const output = data.needData.map((value) => { const { arrayData } = value; const opArrayData = arrayData.filter(item => item.key && item.value &&.item;random). const parsedArray = opArrayData:map((node => ({ needed_key. node,key: needed_value. node;value }))): return { arrayData; parsedArray } }). console;log(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.