jakhando
jakhando

Reputation: 123

How to replace value of first array by value from another array?

I have two arrays with the following structure:

First one: ['banana', 'apple', 'orange']

Second: [{name: 'banana', price: 1}, {name: 'apple', price: 3}, {name: 'orange', price: 4}]

So I want to replace value in first array by price, as a result I want to get

[1, 3, 4]

How can I do this?

Upvotes: 0

Views: 126

Answers (7)

Like this, for example. Should be self-explanatory.

var fruits = ['banana', 'apple', 'orange']
const fruitDataSet = [
 {name: 'banana', price: 1},
 {name: 'apple', price: 3},
 {name: 'orange', price: 4},
 {name: 'ImNotAFruit', price: 4}
]

for(const fruitData of fruitDataSet){
 let index = fruits.findIndex(x => x == fruitData.name)
   if(index != -1){
    fruits.splice(index, 1, fruitData.price)
   }
}

console.log(fruits)

Upvotes: 0

net.uk.sweet
net.uk.sweet

Reputation: 12431

const arr1 = ['banana', 'apple', 'orange'];
const arr2 = [{name: 'banana', price: 1}, {name: 'apple', price: 3}, {name: 'orange', price: 4}];

console.log(arr1.map(fruit => arr2.find(({ name }) => fruit === name).price));

Upvotes: 1

vishnu v
vishnu v

Reputation: 323

Here is a oneliner

let array = [
  {name: 'banana', price: 1}, 
  {name: 'apple', price: 3}, 
  {name: 'orange', price: 4}
 ]

let result = array.reduce((acc,item) => acc.concat(item.price) , [])
alert (result)

Upvotes: -1

shubham jha
shubham jha

Reputation: 1480

We can use map to store the {name->price} by iterating in second array and iterate through the name in first array to return the price array. We could have used two nested loops to get price array, however using map is faster than using two nested loops.

const first = ['banana', 'apple', 'orange']
const second = [{name: 'banana', price: 1}, {name: 'apple', price: 3}, {name: 'orange', price: 4}];
const map = new Map();
second.forEach((item)=>{
  map.set(item.name,item.price);
})

const result = first.map((name)=>map.get(name));
console.log(result)

Upvotes: 0

Lars Flieger
Lars Flieger

Reputation: 2562

Try this:

const products = ['banana', 'apple', 'orange']
const data = [{name: 'banana', price: 1}, {name: 'apple', price: 3}, {name: 'orange', price: 4}]

const prices = products.map(product => {
  return data.find(data => data.name === product).price
})
console.log(products, prices)

Upvotes: 1

TechySharnav
TechySharnav

Reputation: 5094

You can map the second array, and return the price of it.

let arr1 = ['banana', 'apple', 'orange']

let arr2 = [{name: 'banana', price: 1}, {name: 'apple', price: 3}, {name: 'orange', price: 4}]

let arr3 = arr2.map(elem=>{
  if(arr1.indexOf(elem.name) !== -1){
    return elem.price
  }
})

console.log(arr3);

Upvotes: 1

trincot
trincot

Reputation: 351308

If your data gets large, you should probably avoid scanning the whole array of objects over and over again. You can use a Map object to key your data by name -- using its constructor. Then the lookup will run smooth:

let keys = ['banana', 'apple', 'orange'];
let data = [{name: 'banana', price: 1}, {name: 'apple', price: 3}, {name: 'orange', price: 4}];

let map = new Map(data.map(o => [o.name, o]));
let result = keys.map(key => map.get(key).price);

console.log(result);

If your data objects have other properties, you can similarly reach them with map.get(key).otherproperty... Or just the complete object with map.get(key).

Upvotes: 0

Related Questions