0

I have this emails from an api response "emails": "{\"1\": \"help@me.com\", \"2\": \"help2@help.com\", \"3\": \"please@helpme.com\"}"

I tried to push it into an array like

this.data.forEach((item) => {
        dataArr.push([
          Object.values(JSON.parse(item.emails)),
        ])
})

But I'm getting only the first email and not the other emails. how do I solve this problem?

vuenewb
  • 99
  • 9
  • What is `JSON.parse(item.emails)`? And what type is it? And do you want to push it as an array, or as single values (`[[1,2],[3,4]]` vs `[1,2,3,4]`)? Depending on what you want you will need `...Object.values(...)` – A_A Oct 21 '20 at 11:05
  • 1
    What is `this.data`? Is it the "raw" response from the API? Is it a parsed response? Is it something else? – secan Oct 21 '20 at 11:05
  • it is string type i believe like double encoded JSON so I used JSON.parse. I want to show just the emails like and not the key 1, 2, 3 – vuenewb Oct 21 '20 at 11:09
  • @secan yes it's the raw data from API. it's not parsed – vuenewb Oct 21 '20 at 11:12
  • I guess because there are more than one email and i need to loop through emails but right now I'm not doing that and that's why I only get the first email from emails how do I loop through all the emails inside that forEach loop? – vuenewb Oct 21 '20 at 11:12

5 Answers5

0

Use concat, not push Copy array items into another array

this.data.forEach((item) => {
    dataArr = dataArr.concat(
      Object.values(JSON.parse(item.emails))
    )
})
Dmitri Algazin
  • 3,332
  • 27
  • 30
0

Assuming you got a JSON object :

obj = {
     "emails": "{\"1\": \"help@me.com\", \"2\": \"help2@help.com\", \"3\": \"please@helpme.com\"}"
}

You can parse it to a real emails object then for each field add it in an array :

let emails = [];
const jsonemails = JSON.parse(obj.emails);
for (let field in jsonemails)
    emails.push(jsonemails[field]);
Dharman
  • 30,962
  • 25
  • 85
  • 135
damjuve
  • 314
  • 4
  • 10
0

This adds each email to the array:

const data = {
  item1: {
    "id": 1111111,
    "emails": "{\"1\": \"help@me.com\", \"2\": \"help2@help.com\", \"3\": \"please@helpme.com\"}",
    "name": "John Doe"
  },
  item2: {
    "id": 2222222,
    "emails": "{\"1\": \"help@me.com\", \"2\": \"help2@help.com\", \"3\": \"please@helpme.com\"}",
    "name": "Jane Doe"
  }
}
let dataArr = []
let emailArr = []

for (const item in data) {
  const value = data[item]
  const parsedEmails = JSON.parse(value.emails)
  for (const email in parsedEmails) {
    emailArr.push(parsedEmails[email])
  }
  dataArr.push([value.id, emailArr, value.name])
  emailArr = []
}

console.log(dataArr)
Yasmin
  • 343
  • 3
  • 18
  • there are other fields too like id, name. and I need to put the email between id and name, how do I push it that way? – vuenewb Oct 21 '20 at 11:14
  • @vuenewb I've updated the answer based on multiple entries. Is this the data structure you have? – Yasmin Oct 21 '20 at 11:25
  • @vuenewb also you can use `for(const item in data)` for iterating over objects. `data.forEach` won't work for objects. – Yasmin Oct 21 '20 at 11:27
  • in dataArr array, I need the id and name too. email to be between the id and name. something like ```[id, emails, name], [1, [123@gmail.com, 345@gmail.com], myname]``` – vuenewb Oct 21 '20 at 11:28
  • @vuenewb so if I got it right it needs double iteration. I've updated my answer based on your comment. – Yasmin Oct 21 '20 at 11:53
0

Parse the response first, then use the Object.keys function.

let string = <Api Response>;
let parsed = JSON.parse(string);
let emails = Object.values(parsed.emails);
MaatMa
  • 204
  • 1
  • 7
  • 1
    Nope; that would produce an array containing the object keys (`[1, 2, 3]`); you should use `Object.values()` instead. ;) – secan Oct 21 '20 at 12:54
  • Of course, you're right. Should have checked my comment before. – MaatMa Oct 21 '20 at 12:59
0

// your raw data (in your case, 'this.data')
const data = JSON.stringify({
  "emails":{
    "1":"help@me.com",
    "2":"help2@help.com",
    "3":"please@helpme.com",
    "11":"please11@helpme.com"
  }
});

// 1. Parse the response
const parsedData = JSON.parse(data); // in your case 'JSON.parse(this.data);'

// 2. create the array of emails
const emailsArr = Object.values(parsedData.emails);

// 3. loop through the array
emailsArr.forEach(email => {
  console.log(email);
});
secan
  • 2,622
  • 1
  • 7
  • 24