1
[
    {'id': 123, 'name': 'apples', 'total': 30},
    {'id': 541, 'name': 'oranges', 'total': 42},
    {'id': 300, 'name': 'bananas', 'total': 18}
]

How do I output this array in a form like that:

Apples: 30
Oranges: 42
Bananas: 18

I tried

let myArray = [{
    'id': 123,
    'name': 'apples',
    'total': 30
  },
  {
    'id': 541,
    'name': 'oranges',
    'total': 42
  },
  {
    'id': 300,
    'name': 'bananas',
    'total': 18
  }
]
console.log(JSON.stringify(myArray.map(a => a.name)))

but that's not exactly what I want.

4
  • a => a.name+ ':'+ a.total+'\n' Commented Jul 27, 2018 at 11:11
  • @NaeemShaikh I'm assuming you mean to use string template. If yes, try escaping `. Its \` Commented Jul 27, 2018 at 11:11
  • @mikebrsv, question is, what format do you wish to save? String or array? Commented Jul 27, 2018 at 11:14
  • myArray.map(a => `${a.name}: ${a.total}`) Commented Jul 27, 2018 at 11:14

5 Answers 5

1

You can use Object.assign with spread syntax and to create an object from array.

const data = [{'id': 123, 'name': 'apples', 'total': 30},{'id': 541, 'name': 'oranges', 'total': 42},{'id': 300, 'name': 'bananas', 'total': 18}]

const cap = str => str[0].toUpperCase() + str.slice(1)
const result = Object.assign(...data.map(({name, total}) => ({[cap(name)]: total})))
console.log(result)

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

Comments

0

You'll need to iterate the array

let arr = [{
    'id': 123,
    'name': 'apples',
    'total': 30
  },
  {
    'id': 541,
    'name': 'oranges',
    'total': 42
  },
  {
    'id': 300,
    'name': 'bananas',
    'total': 18
  }
]

arr.forEach((element) => {

  // do stuff with data
  let id = element.id;
  let name = element.name;
  let total = element.total;

  console.log(`${name}: ${total}`);

  // or write in div/form with innerHtml etc..
});

Comments

0

You can do something like this:

var data = [
    {'id': 123, 'name': 'apples', 'total': 30},
    {'id': 541, 'name': 'oranges', 'total': 42},
    {'id': 300, 'name': 'bananas', 'total': 18}
]

console.log(data.map(a => ({[a['name'].charAt(0).toUpperCase() + a['name'].slice(1)]: a['total']})));

Comments

0

Your array will be something like this:

var fruits = [
    {'id': 123, 'name': 'apples', 'total': 30},
    {'id': 541, 'name': 'oranges', 'total': 42},
    {'id': 300, 'name': 'bananas', 'total': 18}
]

You can loop it for array using for loop.

for(i=0; i<fruits.length; i++)

{
    console.log(fruits[i].name + " : " + fruits[i].total);
}

It gives output you desire.

enter image description here

Comments

0

you can also do like this

let data=[
    {'id': 123, 'name': 'apples', 'total': 30},
    {'id': 541, 'name': 'oranges', 'total': 42},
    {'id': 300, 'name': 'bananas', 'total': 18}
]

let update=Object.assign(...data.map(item=> ({[item.name.charAt(0).toUpperCase() + item.name.slice(1)]:item.total})))

console.log(update);

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.