Home > other >  how to convert array object into array csv kind of type
how to convert array object into array csv kind of type

Time:09-28

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'

  ]

CodePudding user response:

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);

CodePudding user response:

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(','))
  ]

)

CodePudding user response:

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)

CodePudding user response:

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);

CodePudding user response:

  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);

  • Related