0

I have this array object and I want to try convert it to like array for csv file kind of type. For example like array object bellow

const arrayObject = [
  {
    Name: "Alex",
    Age: 16,
    Address: "Miami",
  },
  {
    Name: "James",
    Age: 36,
    Address: "LA",
  },
  {
    Name: "Mark",
    Age: 25,
    Address: "San Diego",
  },
];

and what I expect to converted to look like this:

 [
    'Name,Age,Address',
    'Alex,16,Miami',
    'James,36,LA',
    'Mark,25,San Diego'

  ]
1
  • What have you tried to achieve this? Or what are your thoughts on this. This helps us to help you better Commented Sep 28, 2021 at 12:22

5 Answers 5

1

Please use these functions. Object.keys(), Object.values(), Array.map(), Array.filter()

const arrayObject = [{
    Name: 'Alex',
    Age: 16,
    Address: 'Miami'
  },
  {
    Name: 'James',
    Age: 36,
    Address: 'LA'
  },
  {
    Name: 'Mark',
    Age: 25,
    Address: 'San Diego',
  }
]

const newKeyArray = arrayObject.map(item => Object.keys(item).toString()).filter((item, index, self) => self.indexOf(item) === index);

const newValueArray = arrayObject.map(item => Object.values(item).toString());

const result = newKeyArray.concat(newValueArray);

console.log(result);

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

Comments

1

let data = [{ Name: 'Alex', Age: 16, Address: 'Miami' }, { Name: 'James', Age: 36, Address: 'LA' }, { Name: 'Mark', Age: 25, Address: 'San Diego',
}]

console.log(

  [
    Object.keys(data[0]).join(','),
    ...data.map(itm => Object.values(itm).join(','))
  ]

)

1 Comment

You're missing the headings.
0

Map the arrayObject variable and return the values as a string. This will work for any other properties you add.

const arrayObject = [{
    Name: 'Alex',
    Age: 16,
    Address: 'Miami'
  },
  {
    Name: 'James',
    Age: 36,
    Address: 'LA'
  },
  {
    Name: 'Mark',
    Age: 25,
    Address: 'San Diego',
  }
]
const newArray = arrayObject.map(item => {
  return Object.values(item).toString();
});

console.log(newArray)

1 Comment

You're missing the headings.
0

Get the keys using Object.keys and the values using Object.values and then use concat to merge them into the final result.

const arrayObject = [{
    Name: 'Alex',
    Age: 16,
    Address: 'Miami'
},
{
    Name: 'James',
    Age: 36,
    Address: 'LA'
},
{
    Name: 'Mark',
    Age: 25,
    Address: 'San Diego',
}];

const keys = Object.keys(arrayObject[0]);
const values = arrayObject.map(item =>  Object.values(item)).toString().split(',');

const res = keys.concat(values);
console.log(res);

Comments

0
  1. Create some headings from the first object's Object.keys.

  2. map over the array and for each object return its Object.values (an array), and join the array up into a comma-delimited string.

  3. Combine the headings and the mapped data into a new array.

const arr=[{Name:'Alex',Age:16,Address:'Miami'},{Name:'James',Age:36,Address:'LA'},{Name:'Mark',Age:25,Address:'San Diego'}];

const headings = Object.keys(arr[0]).join(',');

const rows = arr.map(obj => {
  return Object.values(obj).join(',');
});

const csv = [headings, ...rows];

console.log(csv);

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.