I am very new to js, now i have a json data which was passed by backend to my js file. The json data is like the following:
{
Vivo:{Time:[20190610,20190611],Price:[2000,2000]},
Huawei:{Time:[20190610,20190611],Price:[3000,3000]},
Maxvalue:3000
}
the json data i get is by the following code:
fetch('/Tmall') //Tmall is the url i go to fetch data
.then(function(response) {
return response.json();
}).then(function(Data) {
...
}
Next I will process the data to present at the front-end, but i don't know how to assign the Data to two different argument:
Cellphone =
{
Vivo:{Time:[20190610,20190611],Price:[2000,2000]},
Huawei:{Time:[20190610,20190611],Price:[3000,3000]}
}
Max = {Maxvalue:3000}
Here is what i tried to do, i used if-is the key to separate the data from the original one, but it dosen't work
var Cellphone = {}
for (var i = 0; i < Data.length; i++)
{
if (Data.key[i] != 'Maxvalue'){
Cellphone.push(Data[i])
}
}
You can use Object.keys() combined with Array.prototype.filter() and Array.prototype.map().
Code:
// Your `Data` response...
const Data = {Vivo:{Time:[20190610,20190611],Price:[2000,2000]},Huawei:{Time:[20190610,20190611],Price:[3000,3000]},Maxvalue:3000};
// Logic
const Cellphone = Object.keys(Data)
.filter(k => k !== 'Maxvalue')
.map(k => ({ [k]: Data[k] }));
const Max = { Maxvalue: Data.Maxvalue };
console.log('Cellphone:', Cellphone);
console.log('Max:', Max);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With