1

i have a grouped array like this

this.state.tableData=

[0:{key: "EMAIL",val:  ["[email protected]", "[email protected]", "[email protected]"]}
1:{key: "LASTNAME",val:  ["Smith", "Pierce", "Paige"]}
2:{key: "FIRSTNAME",val: ["John", "Harry", "Howard"]}
3:{key: "SMS",val: ["33123456789", "33111222222", "33777888898"]}
]

i want to convert it to normal array like

0: {EMAIL: "[email protected]", LASTNAME: "Smith", FIRSTNAME: "John", SMS: "33123456789"}
1: {EMAIL: "[email protected]", LASTNAME: "Pierce", FIRSTNAME: "Harry", SMS: "33111222222"}
2: {EMAIL: "[email protected]", LASTNAME: "Paige", FIRSTNAME: "Howard", SMS: "33777888898"}

i have tried following code

this.state.csvHeaders=["EMAIL','FIRSTNAME','LASTNAME']

var contacts=[];
this.state.tableData.map(k => {

        k.val.map(r => {

        Object.keys(this.state.csvHeaders).forEach(key => {

             if(k.val["Col"]==="EMAIL")
             {
                contacts[key]['EMAIL']=r;
             }

        })

        })

    })

but it doesnt work .... any suggestion what more can i modified .how can i store , push them to array ?

6
  • Is key and val inside a JSON object? Commented Nov 6, 2019 at 6:53
  • @Neeraj Verma your this.state.tableData is right json ? or objects of array Commented Nov 6, 2019 at 6:53
  • that's not a proper js datastructure, especially the array is not correctly syntactically: ["Smith", "Pierce", "Paige", Col: "LASTNAME"] - the part Col: "LASTNAME" is not a proper array element. Please edit your question with the correct data structures Commented Nov 6, 2019 at 7:00
  • ok , i removed the col , now please tell me how it can be possible ? Commented Nov 6, 2019 at 7:07
  • 1
    You already had a normal array why can't you use that one? Commented Nov 6, 2019 at 8:04

2 Answers 2

3

You can use reduce and forEach to do this:

var tableData = [
  { key: 'EMAIL',val: ['[email protected]','[email protected]','[email protected]'],},
  { key: 'LASTNAME', val: ['Smith', 'Pierce', 'Paige'] },
  { key: 'FIRSTNAME', val: ['John', 'Harry', 'Howard'] },
  { key: 'SMS',val: ['33123456789', '33111222222', '33777888898'],},
];

console.log(
  tableData.reduce((result, { key, val }) => {
    val.forEach((val, index) => {
      result[index] = result[index] || {};
      result[index][key] = val;
    });
    return result;
  }, [])
);

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

1 Comment

Great solution!:)
3

Something along these lines should work:

    const tableData= [
        {key: "EMAIL",val:  ["[email protected]", "[email protected]", "[email protected]"]},
        {key: "LASTNAME",val:  ["Smith", "Pierce", "Paige"]},
        {key: "FIRSTNAME",val: ["John", "Harry", "Howard"]},
        {key: "SMS",val: ["33123456789", "33111222222", "33777888898"]},
];
const csvHeaders = ['EMAIL','FIRSTNAME','LASTNAME', 'SMS'];

const contacts = [];
csvHeaders.forEach( (header, idx) => {

        tableData[0].val.forEach( (info, contactPos) => {
                contacts[contactPos] = contacts[contactPos] || {};
                contacts[contactPos][header] =  tableData.find(row => row.key === header).val[contactPos];
        })
})

console.log(contacts);

5 Comments

right, I haven't looked very closely to the actual result. I updated the code with what works now :)
now it worked ...perfect solution ... thanks ... yes my syntax need correction .. i will do it
wait . one bug i found .. if i changes key : "EMAIL" to key : "SMS" . .. then still array pushes email data in it . this should not happen
Right, I've updated the code to make it search in tableData by current header.

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.