1

I have an object which consists of newSales and oldSales and I am looping over them to get the key value pairs. I have been able to console log them but now I am stuck. How do I push those key value pairs in my array? This is a sample codepen

const obj = {
  newSales: 1,
  oldSales: 0,
  anotherProp: 'something',
  etc: 'etc',
  values: 'numbers'
}

function myDataSet() {
  let dataSet = []
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      console.log(`${key} : ${obj[key]}`)
      dataSet.push({
        name: ['newSales', 'oldSales'],
        data: [1, 0]
      })
    }
  }
  return dataSet
}

myDataSet();

The new Name in the object should be the name i.e. newSales and oldSales and data inside should be an array with the values 1 and 0. I appreciate all the help.

Expected output:

[ { name: ['newSales', 'oldSales'], data:[ 1,0 ] } ]
10
  • So the final result will be: [ { name: "newSales", data: [1, 0] }, { name: "oldSales", data: [1, 0] }]. That doesn't make sense Commented Nov 8, 2019 at 16:49
  • BTW, since you only have one object and since that object only contains 2 properties, why do you need to loop it in the first place? Just construct the result directly using literals Commented Nov 8, 2019 at 16:51
  • My bad. the final result will be: [ { name: ['newSales', 'oldSales'], data:[ 1,0 ] }. Thank you for pointing that out. I Should have been more clear. Commented Nov 8, 2019 at 16:52
  • Yeah actually it consists of a lot more properties, actually let me fix the code above. Commented Nov 8, 2019 at 16:53
  • @ibrahimmahrir I fixed the code, hopefully it's a bit clear now. Thank you. Commented Nov 8, 2019 at 16:55

4 Answers 4

3

If I understood correctly, you want this.

Edited to have the outer array as required, although I don't understand its purpose.

let dataSet = [{
  name: Object.keys(obj),
  data: Object.values(obj)
}];
Sign up to request clarification or add additional context in comments.

7 Comments

yes, but both name and data should be in their own array.
What do you mean by that?
[ { name: ['newSales', 'oldSales'], data:[ 1,0 ] } ]
It should match that now.
JS objects do not guarantee property iteration order. That means that key-value pairs may break up (your code may result in [ { name: ['newSales', 'oldSales'], data:[ 0,1] } ]. Note that the values are swapped)
|
1

you might want something line this

const obj = {
  newSales: 1,
  oldSales: 0,
  anotherProp: 'something',
  etc: 'etc',
  values: 'numbers'
}


  let dataSet = [], names= [], data =[];
  for (let key in obj) {
    if (obj.hasOwnProperty(key) && ["newSales", "oldSales"].indexOf(key) >= 0) {    
      console.log(`${key} : ${obj[key]}`)
      data.push(obj[key]);
      names.push(key);
    }
  }
  dataSet.push({names:names, data:data});
  
console.log(dataSet)

Comments

0

You can get the list of an object's keys by calling Object.keys(), similarly for values:

const obj = {
  newSales: 1,
  oldSales: 0
}

function myDataSet() {
  let dataSet = []
  dataSet.push({
    'name': Object.keys(obj),
    'data': Object.values(obj)
  });
  return dataSet;
}

console.log(myDataSet());

1 Comment

JS objects do not guarantee property iteration order. That means that key-value pairs may break up (your code may result in [ { name: ['newSales', 'oldSales'], data:[ 0,1 ] } ]. Note that the values are swapped)
0

You could use Object.entries() and Array#reduce():

const obj = {
  newSales: 1,
  oldSales: 0
}
const dataSet=[]

dataSet.push(Object.entries(obj).reduce((acc,[key,value])=>{
  acc.name.push(key)
  acc.data.push(value)
  return acc
},{name:[],data:[]}));

console.log(dataSet);

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.