0

So I have an array that has this structure

[{id:'bitcoin', change:-5.5}]

Again I have another array that has cryptos in this format

[{
            id: 'bitcoin',
            symbol: 'BTC',
            name: 'Bitcoin',
            img: 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bitcoin.svg/1200px-Bitcoin.svg.png',
            addr: '0xECe365B379E1dD183B20fc5f022230C044d51404'
        },]

That is just but an example, the arrays have multiple data objects. I am transforming the crypto array into a new array because I am calculating their real-time market price using each coin address like this

for (let i = 0; i < this.assets.length; i++) {
            coinValue = await getPrice(this.assets[i].addr);
            this.transformedAssets.push({
                id: this.assets[i].id,
                symbol: this.assets[i].symbol,
                name: this.assets[i].name,
                img: this.assets[i].img,
                addr: this.assets[i].addr,
                value: numberWithCommas(coinValue)
            });
        }

Now I also want to pass coin_24h_percentage_change to the new array because the crypto array and the percentage change array have common id that can link them together but I am stranded on how to do that

I did something like this but its not working How am I suppose to make this happen

for (let i = 0; i < this.assets.length; i++) {
            coinValue = await getPrice(this.assets[i].addr);
            const price_change_percentage_24h = JSON.parse(JSON.stringify(this.coinPerfomance[0].change));
            window.console.log(price_change_percentage_24h);
            this.transformedAssets.push({
                id: this.assets[i].id,
                symbol: this.assets[i].symbol,
                name: this.assets[i].name,
                img: this.assets[i].img,
                addr: this.assets[i].addr,
                daychange: this.coinPerfomance[this.assets[i].id].change,
                value: numberWithCommas(coinValue)
            });
        }
MAXWEL OCHIENG
  • 170
  • 1
  • 14
  • Does this answer your question? [JavaScript merging objects by id](https://stackoverflow.com/questions/19480008/javascript-merging-objects-by-id) – Pablo Silió Jul 18 '22 at 08:54
  • `daychange: this.coinPerfomance[this.assets[i].id].change,` - so `this.coinPerfomance` here is supposed to be that first array you have shown us? Trying to access that with `this.assets[i].id` can't work - it is a numerically zero-based indexed array. But you could convert it into in object first, that uses the `id` values as properties, and the `change` values as the object values. – CBroe Jul 18 '22 at 08:56
  • so how can I do That – MAXWEL OCHIENG Jul 18 '22 at 08:59
  • You can of course convert the array to an object yourself in a loop - or you can just use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries – CBroe Jul 18 '22 at 09:08

1 Answers1

2

You can try something like this :

let priceChange = [{id:'bitcoin', change:-5.5}];

let data = [
    {
        id: 'bitcoin',
        symbol: 'BTC',
        name: 'Bitcoin',
        img: 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bitcoin.svg/1200px-Bitcoin.svg.png',
        addr: '0xECe365B379E1dD183B20fc5f022230C044d51404'
    }
];


let result = data.map(el => ({...el, 'change': priceChange.find(pc => pc.id == el.id).change}));
console.log(result);
nem0z
  • 1,060
  • 1
  • 4
  • 17
  • thanks to you I did this but I am on Vue js actually and this added observer in my results array and it's always a pain in the ass ? When I convert it into string and the object using JSON(parse and stringify) methods it only transform it at the moment but not forever how can I fix that – MAXWEL OCHIENG Jul 18 '22 at 10:30