0

I have this form of data coming from an API and I want it to convert into different structure in JavaScript .

[
  {
    "1": {
      "id": 1,
      "coinname": "Binance Coin",
      "symbol": "BNB",
      "owner": "NULL",
      "repo": "NULL",
      "language": "NULL",
      "total_public_repos": 0,
      "tosymbol": "BUSD",
      "exchanges": "Binance",
      "coinlimit": "-1",
      "category": "coin",
      "domain": "cryptocurrency",
      "country": "china",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "BFT",
      "Whitepaper": "",
      "website": "https://www.binance.com/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "n/a",
      "ExchangesCount": 25,
      "platform": "bsc"
    },
    "2": {
      "id": 2,
      "coinname": "Bitcoin",
      "symbol": "BTC",
      "owner": "Bitcoin",
      "repo": "bitcoin",
      "language": "C++",
      "total_public_repos": 4,
      "tosymbol": "USD",
      "exchanges": "Coinbase",
      "coinlimit": "20,999,999.98",
      "category": "Currency",
      "domain": "cryptocurrency",
      "country": "japan",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "PoW",
      "Whitepaper": "https://www.cryptocompare.com/media/37745820/bitcoin.pdf",
      "website": "https://bitcoin.org/en/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "SHA-256",
      "ExchangesCount": 72,
      "platform": null
    }
  },
  {
    "1": {
      "id": 1,
      "price": 400.6,
      "marketcap": 67355696581.54001,
      "timestamp": 1651737822,
      "datetime": "2022-05-05 08:03:42"
    },
    "2": {
      "id": 2,
      "price": 39538.5,
      "marketcap": 752463351490.5,
      "timestamp": 1651737817,
      "datetime": "2022-05-05 08:03:37"
    }
  },
  {
    "1": {
      "id": 1,
      "last30dcommits": null,
      "activity": "No Github Repo Found"
    },
    "2": {
      "id": 2,
      "last30dcommits": 154,
      "activity": "High Activity"
    }
  },
  {
    "1": {
      "id": 1,
      "change24h": 2.87,
      "change7d": 2.12,
      "change30d": -13.41,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600
    },
    "2": {
      "id": 2,
      "change24h": 2.26,
      "change7d": 0.47,
      "change30d": -18.04,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600
    }
  },
  {
    "1": {
      "tweetsRating": 14.36,
      "newsRating": 34.4
    },
    "2": {
      "tweetsRating": 16.41,
      "newsRating": 43.44
    }
  },
  {
    "1": {
      "score": 70
    },
    "2": {
      "score": 93.33
    }
  }
]

this is the data I am receiving . Now I want a data like this

[
   {
      "id": 1,
      "coinname": "Binance Coin",
      "symbol": "BNB",
      "owner": "NULL",
      "repo": "NULL",
      "language": "NULL",
      "total_public_repos": 0,
      "tosymbol": "BUSD",
      "exchanges": "Binance",
      "coinlimit": "-1",
      "category": "coin",
      "domain": "cryptocurrency",
      "country": "china",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "BFT",
      "Whitepaper": "",
      "website": "https://www.binance.com/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "n/a",
      "ExchangesCount": 25,
      "platform": "bsc",
      "price": 400.6,
      "marketcap": 67355696581.54001,
      "timestamp": 1651737822,
      "datetime": "2022-05-05 08:03:42",
      "last30dcommits": null,
      "activity": "No Github Repo Found",
      "change24h": 2.87,
      "change7d": 2.12,
      "change30d": -13.41,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600,
    "tweetsRating": 14.36,
      "newsRating": 34.4,
    "score": 70
    },
     {
      "id": 2,
      "coinname": "Bitcoin",
      "symbol": "BTC",
      "owner": "Bitcoin",
      "repo": "bitcoin",
      "language": "C++",
      "total_public_repos": 4,
      "tosymbol": "USD",
      "exchanges": "Coinbase",
      "coinlimit": "20,999,999.98",
      "category": "Currency",
      "domain": "cryptocurrency",
      "country": "japan",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "PoW",
      "Whitepaper": "https://www.cryptocompare.com/media/37745820/bitcoin.pdf",
      "website": "https://bitcoin.org/en/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "SHA-256",
      "ExchangesCount": 72,
      "platform": null,
    "price": 400.6,
      "marketcap": 67355696581.54001,
      "timestamp": 1651737822,
      "datetime": "2022-05-05 08:03:42",
      "last30dcommits": null,
      "activity": "No Github Repo Found",
      "change24h": 2.87,
      "change7d": 2.12,
      "change30d": -13.41,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600,
      "tweetsRating": 14.36,
      "newsRating": 34.4,
    "score": 70
    }
   
]

For clarification . The 1 , 2 3,...etc are the coin id of each coin and the data coming in previous code is coming from 6 sources . hence its coming as object of objects and that too separately . Now I want it this way where for each coin all the data is segregated to one single object . Hope I am clear in terms of the problem . Any help would be highly appreciable.

2
  • Try this one , const newArr = arr.reduce((acc, c) => { const keys = Object.keys(c); for (i of keys) acc.push(c[i]); return acc; }, []); Commented May 7, 2022 at 7:52
  • Its not adding properties for the same coin from other objects together . Few changes is required in this code. BTW thanks for the help . Commented May 7, 2022 at 8:03

3 Answers 3

1

This code I think that it works like you want.

const dataIN = [
  {
    "1": {
      "id": 1,
      "coinname": "Binance Coin",
      "symbol": "BNB",
      "owner": "NULL",
      "repo": "NULL",
      "language": "NULL",
      "total_public_repos": 0,
      "tosymbol": "BUSD",
      "exchanges": "Binance",
      "coinlimit": "-1",
      "category": "coin",
      "domain": "cryptocurrency",
      "country": "china",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "BFT",
      "Whitepaper": "",
      "website": "https://www.binance.com/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "n/a",
      "ExchangesCount": 25,
      "platform": "bsc"
    },
    "2": {
      "id": 2,
      "coinname": "Bitcoin",
      "symbol": "BTC",
      "owner": "Bitcoin",
      "repo": "bitcoin",
      "language": "C++",
      "total_public_repos": 4,
      "tosymbol": "USD",
      "exchanges": "Coinbase",
      "coinlimit": "20,999,999.98",
      "category": "Currency",
      "domain": "cryptocurrency",
      "country": "japan",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "PoW",
      "Whitepaper": "https://www.cryptocompare.com/media/37745820/bitcoin.pdf",
      "website": "https://bitcoin.org/en/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "SHA-256",
      "ExchangesCount": 72,
      "platform": null
    }
  },
  {
    "1": {
      "id": 1,
      "price": 400.6,
      "marketcap": 67355696581.54001,
      "timestamp": 1651737822,
      "datetime": "2022-05-05 08:03:42"
    },
    "2": {
      "id": 2,
      "price": 39538.5,
      "marketcap": 752463351490.5,
      "timestamp": 1651737817,
      "datetime": "2022-05-05 08:03:37"
    }
  },
  {
    "1": {
      "id": 1,
      "last30dcommits": null,
      "activity": "No Github Repo Found"
    },
    "2": {
      "id": 2,
      "last30dcommits": 154,
      "activity": "High Activity"
    }
  },
  {
    "1": {
      "id": 1,
      "change24h": 2.87,
      "change7d": 2.12,
      "change30d": -13.41,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600
    },
    "2": {
      "id": 2,
      "change24h": 2.26,
      "change7d": 0.47,
      "change30d": -18.04,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600
    }
  },
  {
    "1": {
      "tweetsRating": 14.36,
      "newsRating": 34.4
    },
    "2": {
      "tweetsRating": 16.41,
      "newsRating": 43.44
    }
  },
  {
    "1": {
      "score": 70
    },
    "2": {
      "score": 93.33
    }
  }
];

function Question21() {
  const positionsId = {};
  const dataOUT = [];
  dataIN.forEach((element) => {
    const ids = Object.keys(element);
    ids.forEach((idKey) => {
      const objectInfo = element[idKey];
      const posDataOUT = positionsId[idKey];
      if (posDataOUT === undefined) {
        positionsId[idKey] = dataOUT.length;
        dataOUT.push(objectInfo);
      } else {
        dataOUT[posDataOUT] = { ...dataOUT[posDataOUT], ...objectInfo };
      }
    });
  });
  console.log(dataOUT);

  return (
    <div>Question 21 - check console</div>
  );
}

export default Question21;

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

1 Comment

Thanks a lott man . You're a saviour.. It worked .
0

This should also work:

const data=[{"1": {"id": 1, "coinname": "Binance Coin", "symbol": "BNB", "owner": "NULL", "repo": "NULL", "language": "NULL", "total_public_repos": 0, "tosymbol": "BUSD", "exchanges": "Binance", "coinlimit": "-1", "category": "coin", "domain": "cryptocurrency", "country": "china", "logo": "", "flag": "", "Product_Live": null, "Funds_Raised": null, "Sale_type": "BFT", "Whitepaper": "", "website": "https://www.binance.com/", "type": "blockchain", "launched": null, "Algo_used": "n/a", "ExchangesCount": 25, "platform": "bsc"} , "2": {"id": 2, "coinname": "Bitcoin", "symbol": "BTC", "owner": "Bitcoin", "repo": "bitcoin", "language": "C++", "total_public_repos": 4, "tosymbol": "USD", "exchanges": "Coinbase", "coinlimit": "20,999,999.98", "category": "Currency", "domain": "cryptocurrency", "country": "japan", "logo": "", "flag": "", "Product_Live": null, "Funds_Raised": null, "Sale_type": "PoW", "Whitepaper": "https://www.cryptocompare.com/media/37745820/bitcoin.pdf", "website": "https://bitcoin.org/en/", "type": "blockchain", "launched": null, "Algo_used": "SHA-256", "ExchangesCount": 72, "platform": null} } , {"1": {"id": 1, "price": 400.6, "marketcap": 67355696581.54001, "timestamp": 1651737822, "datetime": "2022-05-05 08:03:42"} , "2": {"id": 2, "price": 39538.5, "marketcap": 752463351490.5, "timestamp": 1651737817, "datetime": "2022-05-05 08:03:37"} } , {"1": {"id": 1, "last30dcommits": null, "activity": "No Github Repo Found"} , "2": {"id": 2, "last30dcommits": 154, "activity": "High Activity"} } , {"1": {"id": 1, "change24h": 2.87, "change7d": 2.12, "change30d": -13.41, "timestamp24h": 1651651200, "timestamp7d": 1651132800, "timestamp30d": 1649145600} , "2": {"id": 2, "change24h": 2.26, "change7d": 0.47, "change30d": -18.04, "timestamp24h": 1651651200, "timestamp7d": 1651132800, "timestamp30d": 1649145600} } , {"1": {"tweetsRating": 14.36, "newsRating": 34.4} , "2": {"tweetsRating": 16.41, "newsRating": 43.44} } , {"1": {"score": 70} , "2": {"score": 93.33} }];
const res={};
data.forEach(c=>
 Object.entries(c).forEach(([id,ob])=>{
  let curr=(res[id]=res[id]||{});
  Object.entries(ob).forEach(([k,v])=>res[id][k]=v)
 })
)
console.log(res)

There are two nested loops:

  • the outer one over all ids
  • and the inner one over all properties of the objects found in the outer loop

Comments

0

// we'll call this below at end, hoisting here for visibility
function processAPI(obj) {
  var table = {};
  var final = [];
  for (var prop in obj) {
    var item = obj[prop];
    var group = table[prop];
    if (!group) {
      group = table[prop] = {};
      final.push(group);
    }
    for (var key in item) {
      group[key] = item[key];
    }
  }
  return final;
}

var apiFeed = [{
    "1": {
      "id": 1,
      "coinname": "Binance Coin",
      "symbol": "BNB",
      "owner": "NULL",
      "repo": "NULL",
      "language": "NULL",
      "total_public_repos": 0,
      "tosymbol": "BUSD",
      "exchanges": "Binance",
      "coinlimit": "-1",
      "category": "coin",
      "domain": "cryptocurrency",
      "country": "china",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "BFT",
      "Whitepaper": "",
      "website": "https://www.binance.com/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "n/a",
      "ExchangesCount": 25,
      "platform": "bsc"
    },
    "2": {
      "id": 2,
      "coinname": "Bitcoin",
      "symbol": "BTC",
      "owner": "Bitcoin",
      "repo": "bitcoin",
      "language": "C++",
      "total_public_repos": 4,
      "tosymbol": "USD",
      "exchanges": "Coinbase",
      "coinlimit": "20,999,999.98",
      "category": "Currency",
      "domain": "cryptocurrency",
      "country": "japan",
      "logo": "",
      "flag": "",
      "Product_Live": null,
      "Funds_Raised": null,
      "Sale_type": "PoW",
      "Whitepaper": "https://www.cryptocompare.com/media/37745820/bitcoin.pdf",
      "website": "https://bitcoin.org/en/",
      "type": "blockchain",
      "launched": null,
      "Algo_used": "SHA-256",
      "ExchangesCount": 72,
      "platform": null
    }
  },
  {
    "1": {
      "id": 1,
      "price": 400.6,
      "marketcap": 67355696581.54001,
      "timestamp": 1651737822,
      "datetime": "2022-05-05 08:03:42"
    },
    "2": {
      "id": 2,
      "price": 39538.5,
      "marketcap": 752463351490.5,
      "timestamp": 1651737817,
      "datetime": "2022-05-05 08:03:37"
    }
  },
  {
    "1": {
      "id": 1,
      "last30dcommits": null,
      "activity": "No Github Repo Found"
    },
    "2": {
      "id": 2,
      "last30dcommits": 154,
      "activity": "High Activity"
    }
  },
  {
    "1": {
      "id": 1,
      "change24h": 2.87,
      "change7d": 2.12,
      "change30d": -13.41,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600
    },
    "2": {
      "id": 2,
      "change24h": 2.26,
      "change7d": 0.47,
      "change30d": -18.04,
      "timestamp24h": 1651651200,
      "timestamp7d": 1651132800,
      "timestamp30d": 1649145600
    }
  },
  {
    "1": {
      "tweetsRating": 14.36,
      "newsRating": 34.4
    },
    "2": {
      "tweetsRating": 16.41,
      "newsRating": 43.44
    }
  },
  {
    "1": {
      "score": 70
    },
    "2": {
      "score": 93.33
    }
  }
]

// process the 1st item in the provided list... in theory, they may provide multiple items to you... here we're only doing one
var data = processAPI(apiFeed[0]);

console.log("data", data);

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.