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)
            });
        }
4
  • Does this answer your question? JavaScript merging objects by id Commented Jul 18, 2022 at 8: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. Commented Jul 18, 2022 at 8:56
  • so how can I do That Commented Jul 18, 2022 at 8:59
  • You can of course convert the array to an object yourself in a loop - or you can just use developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jul 18, 2022 at 9:08

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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

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.