Simple:
var data = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
var result = data.map(function(val) {
return val.location_id;
}).join(',');
console.log(result)
I assume you wanted a string, hence the .join(','), if you want an array simply remove that part.
Simple:
var data = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
var result = data.map(function(val) {
return val.location_id;
}).join(',');
console.log(result)
I assume you wanted a string, hence the .join(','), if you want an array simply remove that part.
You could add brackets to the string, parse the string (JSON.parse) and map (Array#map) the property and the join (Array#join) the result.
var string = '{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}',
array = JSON.parse('[' + string + ']'),
result = array.map(function (a) { return a.location_id; }).join();
console.log(result);
This can be achieved via the join() method which is built into the Array type:
const object = {
"id" : "122223232244",
"title" : "התרעת פיקוד העורף",
"data" : ["עוטף עזה 218","עוטף עזה 217"]
}
/* Join elements of data array in object to a comma separated string */
const value = object.data.join();
console.log(value);
If no separator argument is supplied, then the join() method will default to use a comma separator by default.
Update
If the JSON was supplied in raw text via a string you can use the JSON.parse() method to extract an object from the JSON string value as a first step like so:
const json = `{"id" : "122223232244","title" : "התרעת פיקוד העורף","data" : ["עוטף עזה 218","עוטף עזה 217"]}`
/* Parse input JSON string */
const object = JSON.parse(json);
/* Join elements of data array in object to a comma separated string */
const value = object.data.join();
console.log(value);
Access object properties using dot notation (e.g. obj.data) and then on the array you can use join to convert to a string with a comma in between.
const obj = {
"id" : "122223232244",
"title" : "התרעת פיקוד העורף",
"data" : ["עוטף עזה 218","עוטף עזה 217"]
}
console.log(obj.data.join(', '))
As simple as this - make an array from this objects
var param = '{"key1":"value1","key2":"value2"},{"key3":"value3"}';
var obj = JSON.parse('[' + param + ']');
var objA = obj[0];
var objB = obj[1];
Like this
var data = JSON.parse("[" + param "]");
objA = data[0];
objB = data[1];
You can do the following,
data = [
{'id': 1,
'name': 'book',
'colors': 'red, yellow, blue'
},
{'id': 2,
'name': 'book',
'colors': 'red, yellow, blue'
}
];
ret = data.map((item) => {
return {...item, colors: item.colors.split(',').map(item => item.trim())};
})
console.log(ret);
Strings get into arrays in each object.
let arr = [
{id: 1,
name: 'book',
colors: 'red, yellow, blue'
},
{id: 2,
name: 'book',
colors: 'red, yellow, blue'
}
]
for (let index in arr) {
let colorsIntoArr = arr[index].colors.split(',');
arr[index].colors = colorsIntoArr;
}
/*
[
{
"id": 1,
"name": "book",
"colors": [
"red",
" yellow",
" blue"
]
},
{
"id": 2,
"name": "book",
"colors": [
"red",
" yellow",
" blue"
]
}
]
*/
console.log(arr)
var json = [];
var to = '[email protected],[email protected],[email protected]';
var toSplit = to.split(",");
for (var i = 0; i < toSplit.length; i++) {
json.push({"email":toSplit[i]});
}
Try this ES6 Version which has better perform code snippet.
'use strict';
let to = '[email protected],[email protected],[email protected]';
let emailList = to.split(',').map(values => {
return {
email: values.trim(),
}
});
console.log(emailList);
Use Object.keys with Array#map
The
Object.keys()method returns an array of a given object's own enumerable properties.
The
map()method creates a new array with the results of calling a provided function on every element in this array.
var input = {
"data": {
"source1": "source1val",
"source2": "source2val"
}
};
var output = Object.keys(input.data).map(function(k) {
return input.data[k];
}).join(',');
console.log(output); //manipulated object
console.log(input); //Original object
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
var input = {
"data": {
"source1": "source1val",
"source2": "source2val"
}
};
var output = [];
var i;
for (i = 0; i < input.data.length; i++) {
output.push(input.data[i]);
}
This is an invalid notation - either JavaScript object or JSON. If you can fix your input or can make someone fix it, then it is definitely better to make your data source be valid.
However, sometimes we have to work with wrong data (external providers etc.), then you can make it a valid JSON array by adding a couple brackets in the beginning and the end:
var str = '{ "name":"a", "surname":"b" }, { "name":"c", "surname":"d" }, { "name":"e", "surname":"f" }';
var arr = JSON.parse("[" + str + "]");
//console.log(arr);
for (var i = 0; i < arr.length; i++)
{
console.log("Name #" + (i + 1) + ": " + arr[i].name);
console.log("Surname #" + (i + 1) + ": " + arr[i].surname);
}
It can look a little bit hacky, but it is the best thing you can do when you have to work with such input.
It looks much better than trying to split an object by commas manually, at least for me.
I have one function to split your Array of Objects in many Arrays as you want, see the code :)
var BATCH_SIZE = 2;
var fullList = [{name:"a",surname:"e"},{name:"a",surname:"e"}
, {name:"a",surname:"e"},{name:"a",surname:"e"}, {name:"a",surname:"e"}];
Batchify(fullList,BATCH_SIZE);
function Batchify(fullList,BATCH_SIZE){
var batches = [];
var currentIndex = 0;
while (currentIndex < fullList.length) {
var sliceStart = currentIndex;
var sliceEnd = currentIndex + BATCH_SIZE;
batches.push(fullList.slice(sliceStart, sliceEnd));
currentIndex += BATCH_SIZE;
}
console.log(batches);
}
I have example of code in JSfiddle, I hope solve your problem! :)