js array of objects to string code example

Example 1: return an array of strings from an array of objects js

var items = [{
  id: 1,
  name: 'AAA'
}, {
  id: 2,
  name: 'BBB'
}, {
  id: 3,
  name: 'CCC'
}];

var names = items.map(function(item) {
  return item['name'];
});

console.log(names);
console.log(items);

Example 2: convert array of string to array of objects javascript

myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));

Example 3: convert array object to string javascript

var ar = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; 
console.log( ar.join('') ); // abcdefg
console.log( ar.join(' : ') ); // a : b : c : d : e : f : g
console.log( ar.join('-') ); // a-b-c-d-e-f-g
console.log( ar.join('|') ); // a|b|c|d|e|f|g

Example 4: js array with objects to string

var array = new Array(); //new Array not necessary
var string;

array[0] = {key: value};
array[1] = {key1: value1};

string = JSON.stringify(array);

/*
string should look like
[{"key":"value"},{"key1":"value1"}]
*/