Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript es6 find

const originalData = [
  { "investor": "Sue",   "value":  5,   "investment": "stocks"  },
  { "investor": "Rob",   "value": 15,   "investment": "options" },
  { "investor": "Sue",   "value": 25,   "investment": "savings" },
  { "investor": "Rob",   "value": 15,   "investment": "savings" },
  { "investor": "Sue",   "value":  2,   "investment": "stocks"  },
  { "investor": "Liz",   "value": 85,   "investment": "options" },
  { "investor": "Liz",   "value": 16,   "investment": "options" },
];

const newData = [
  { "investor": "Sue",   "stocks": 0,   "options": 0,   "savings": 0 },
  { "investor": "Rob",   "stocks": 0,   "options": 0,   "savings": 0 },
  { "investor": "Liz",   "stocks": 0,   "options": 0,   "savings": 0 },
];

for (let {investor, value, investment} of originalData) {
  newData.find(x => x.investor === investor)[investment] += value;
}

console.log(newData);
Comment

PREVIOUS NEXT
Code Example
Javascript :: react router dom v6 
Javascript :: angular get name of component 
Javascript :: JavaScript try...catch in setTimeout 
Javascript :: javascript foreach arrow function 
Javascript :: append textarea jquery with value 
Javascript :: js map key value 
Javascript :: open another page js 
Javascript :: mongoose count 
Javascript :: javascript arr.flat 
Javascript :: check if s3 bucket exists in lambda 
Javascript :: flutter http get json to map 
Javascript :: fizz buzz program in javascript 
Javascript :: react enzyme 
Javascript :: getting te value of select multiple value 
Javascript :: react bootsrap color picker 
Javascript :: global axios vue 2 
Javascript :: javascript trigger function when element is in viewport 
Javascript :: socket..io 
Javascript :: show a div in jquery 
Javascript :: vue js hooks 
Javascript :: remove substring from string liquid shopify 
Javascript :: react native flatlist container style 
Javascript :: javascript full date as string 
Javascript :: vuelidate required if another props 
Javascript :: email validation node js 
Javascript :: npm ERR! Error: connect ECONNREFUSED 
Javascript :: how to change input value in javascript using class 
Javascript :: timing code in javascript 
Javascript :: navbar routing in react 
Javascript :: super class js 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =