0

let mything = {
  "holders": [{
    "address": "0xbe0eb53f46cd790cd13851d5eff43d12404d33e8",
    "balance": 8.623839536582375e24,
    "share": 52.02
  }, {
    "address": "0xf977814e90da44bfa03b6295a0616a897441acec",
    "balance": 4.5e24,
    "share": 27.14
  }]
};

let m = Object.entries(mything);
console.log(m);

The above is a json data, stored in a file, now what I want to do is to loop over this whole file which has 2000 of such entries, get just the address part of each entry and append it in a url, so how would I do the looping part?? Any code Snippet for javaScript would be lovely. Cudos.

2
  • 2
    Please add the code you have tried also and your specific challenge with it Commented Dec 9, 2021 at 13:00
  • Thank You Mark, for reaching out, my challenge is to get the address value and further use it, and im trying something like Object.entries() method and after that I dont really know what to do. Commented Dec 9, 2021 at 13:08

4 Answers 4

2

You can use the map function to get a one liner :

const data = {"holders": [{
  "address": "0xbe0eb53f46cd790cd13851d5eff43d12404d33e8",
  "balance": 8.623839536582375e24,
  "share": 52.02
},{
  "address": "0xf977814e90da44bfa03b6295a0616a897441acec",
  "balance": 4.5e24,
  "share": 27.14
}]};

const url = "https://my.url/";
const urls = data.holders.map(holder => `${url}${holder.address}`);

console.log(urls);

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

Comments

0

Since holders object is an array, you can loop over it like below, and make use of the address like constructing the URL as per your logic inside the loop. Here's the example of storing the addresses in an array:

var original = {
  "holders": [{
    "address": "0xbe0eb53f46cd790cd13851d5eff43d12404d33e8",
    "balance": 8.623839536582375e24,
    "share": 52.02
  }, {
    "address": "0xf977814e90da44bfa03b6295a0616a897441acec",
    "balance": 4.5e24,
    "share": 27.14
  }]
};

var addresses = [];
for (let holder of original.holders) {
  addresses.push(holder.address);
}
console.log(addresses)

Comments

0
const data = {"holders": [{
  "address": "0xbe0eb53f46cd790cd13851d5eff43d12404d33e8",
  "balance": 8.623839536582375e24,
  "share": 52.02
},{
  "address": "0xf977814e90da44bfa03b6295a0616a897441acec",
  "balance": 4.5e24,
  "share": 27.14
}]};

for(const obj of Object.values(data)) {
    for(const arr of obj) {
        // Your code
    }
}

Comments

0

Use Array.prototype.map.

const holders = [{
    "address": "0xbe0eb53f46cd790cd13851d5eff43d12404d33e8",
    "balance": 8.623839536582375e24,
    "share": 52.02
}, {
    "address": "0xf977814e90da44bfa03b6295a0616a897441acec",
    "balance": 4.5e24,
    "share": 27.14
}];

const addresses = holders.map((holder) => holder.address);

console.log(addresses);

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.