7

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])
      }
    }
4
  • What should the output be? Commented Jun 11, 2019 at 8:38
  • 2
    Access an object's data with dot notation: let Cellphone = { Vivo: Data.Vivo, Huawei: Data.Huawei }; and let Max = Data.Maxvalue;. Commented Jun 11, 2019 at 8:39
  • @ZivBen-Or How is this a duplicate? Commented Jun 11, 2019 at 8:45
  • @JackBashford The second last code block is the output i want :) the last code block i wrote is only deal with the cellphone part. in reality i actually have multiple cellphone infos need to deal with Commented Jun 11, 2019 at 9:07

5 Answers 5

2

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

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

4 Comments

Hi is "k" a reserved word? or in filter() or map() it can be automatically identified as a variable to iterate through the parameter? i have no idea how machine can realize what k is here
@TileiLiu k is a variable name.. The current element being processed in the array of keys
is it adaptable if i replace 'k' with 'a','b','c' .... or even a word like 'element'?
Yes, that is right.. I used k because referring to the object keys but you can use any variable name
2

Just extract the Maxvalue property, and assign the others to Cellphone. This is really simple with destructuring and spreading:

const data = {
  Vivo: {
    Time: [20190610, 20190611],
    Price: [2000, 2000]
  },
  Huawei: {
    Time: [20190610, 20190611],
    Price: [3000, 3000]
  },
  Maxvalue: 3000
};

const { Maxvalue: Max, ...Cellphone } = data;
console.log("Max:", Max);
console.log("Cellphone:", Cellphone);
.as-console-wrapper { max-height: 100% !important; top: auto; }

3 Comments

HI~I don't get what is this mean "const { Maxvalue: Max, ...Cellphone } = data;"
The const {...} = data means we're extracting object properties. The Maxvalue: Max makes a new variable called Max and gives it the value of data.Maxvalue. The ...Cellphone makes a new object called Cellphone with all the rest of the properties in data.
Hi i put the code in my js file, yet it still dosen't work. is it because instead of an const my "Data" is actually a object?
1

You can individually pick out the max value, save it in to a variable, then remove it from the object. This will allow you to assign all cellphones to a variable, even if their names change. For example;

let data = {
  Vivo:{Time:[20190610,20190611],Price:[2000,2000]},
  Huawei:{Time:[20190610,20190611],Price:[3000,3000]},
  Maxvalue:3000
}

// save the maxValue key
let maxValue = data.Maxvalue;

// then remove it from the object
delete(data.Maxvalue);

// the remaining keys are cellphones
let cellPhones = data;

console.log(maxValue, cellPhones);

1 Comment

Thank you! it's also a great answer.
0

In the function, where you set Data as a parameter, do the following

let Cellphone = {
    Vivo: Data.Vivo,
    Huawei: Data.Huawei
};
let Max = Data.Maxvalue;

2 Comments

What if the cellphone names change?
Capital case in js usually using for class
0

The following code should solve your problem

var phones = {};
var max;
Object.entries(data).forEach(function (entry) {
  if (entry[0] == "Maxvalue") {
    max = entry[1];
  }
  else {
    phones[entry[0]] = entry[1];
  }
});
console.log("Phones: ", phones);
console.log("Max: ", max)

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.