JavaScript
x
45
45
1
let data = {
2
"needData": [
3
{
4
arrayData: [
5
{
6
"key": "dummy",
7
"value": "needed value"
8
},
9
{
10
"key": "secret",
11
"random": "secret_random"
12
},
13
{
14
"key": "another",
15
"value": "testing"
16
},
17
{
18
"key": "another_Secret",
19
"random": "testing_value"
20
},
21
]
22
}
23
]
24
};
25
let json, testing;
26
data.needData.map((value) => {
27
json = {
28
arrayData: [
29
{
30
needed_key: value.arrayData[0].key,
31
needed_value: value.arrayData[0].value,
32
},
33
],
34
};
35
testing = {
36
arrayData: [
37
{
38
needed_key: value.arrayData.key,
39
needed_value: value.arrayData.value,
40
},
41
],
42
};
43
});
44
console.log("value got by running json", json);
45
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
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
I can use manual code to fetch the data but what if data is more and I don’t want to use manual code
Expected Output
JavaScript
1
14
14
1
{
2
"arrayData": [
3
{
4
"needed_key": "dummy",
5
"needed_value": "needed value"
6
},
7
{
8
"needed_key": "another",
9
"needed_value": "testing"
10
}
11
]
12
}
13
14
Advertisement
Answer
This gives your output:
JavaScript
1
9
1
var output = {
2
'arrayData': data.needData[0].arrayData
3
.filter(x => !Object.keys(x).includes('random'))
4
.map(x => {
5
return {needed_key: x['key'], needed_value: x['value']}
6
})
7
}
8
console.log(output);
9