0

I have variable that contain array inside, when i was tried to print it with javascript console.log(res) show like this:

res = [{sakti: "23"},{Baim: "20"},{Jaka: "18"}]

How i suppose to do, if i want to change the data type into like this:

res = [{name: "sakti", y: 23},{name: "Baim", y: 20},{name: "Jaka", y: 18}]

my current code:

this.categoryservice.getRole().subscribe((res)=>{
  console.log(res);
})

5 Answers 5

1

You can use map and Object.keys

let res = [{sakti: "23"},{Baim: "20"},{Jaka: "18"}]

let op = res.map(e=>{
  let key = Object.keys(e)[0]
  return { name: key, y: +e[key] }
})

console.log(op)

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

5 Comments

yeah thanks sir @Code Maniac, but how about convert string number with double quotes into integer without double quotes ?
You can do by parseInt or, implicit conversion. look at the + in y property value i added it for implicit conversion to number
@tonjel "but how about convert string number with double quotes into integer without double quotes" How is that inquiry related to the original question?
Perfect sir @CodeManiac, thanks it so helpful. so it does need +
@tonjel yes, but i will suggest you to read more about string to number conversion that will help in long run, consider marking answer as correct one if it helped and solved your problem
0

You can do this with Array.map, Object.entries and destructuring assignment:

const data = [{sakti: "23"}, {Baim: "20"}, {Jaka: "18"}];

const result = data.map(item => {
  const [key, value] = Object.entries(item)[0];
  return { name: key, y: value };
});

console.log(result);

Comments

0

Array.from is another way of mapping the object array into a new array of objects by using the second mapping argument. Object.keys & Object.values can be used to construct the new object by taking the [0] position from the key array which will be the name and [0] from the value array which will be the y key.

const res = [{sakti: "23"},{Baim: "20"},{Jaka: "18"}]
const arrayConv = Array.from(res, obj => { return {"name":Object.keys(obj)[0], "y":Object.values(obj)[0] } });
console.log(arrayConv);

Comments

0

you can use map and object.entries for this

 var res = [{sakti: "23"},{Baim: "20"},{Jaka: "18"}]

 var result = res.map((i)=>{
        let obj = Object.entries(i);
        return {'name': obj[0][0], 'y': obj[0][1]};
    });
console.log(result);

Comments

0

With the new experimental flatMap() you can create a generic approach (in case one of your object have more than one key:val pair):

const res = [{sakti: "23", foo: "33"},{Baim: "20"},{Jaka: "18"}];

let mapped = res.flatMap(o => Object.entries(o).map(([k, v]) => ({name: k, y: +v})));

console.log(mapped);

But, you can always use reduce() for this too:

const res = [{sakti: "23", foo: "33"},{Baim: "20"},{Jaka: "18"}];

let mapped = res.reduce(
    (acc, o) => acc.concat(Object.entries(o).map(([k, v]) => ({name: k, y: +v}))),
    []
);

console.log(mapped);

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.