最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting a comma separated list of json objects to an array - Stack Overflow

programmeradmin2浏览0评论

I have a problem with a received javascript object. I receive something like this:

{
 "name":"a",
 "surname":"b"
},
{
 "name":"c",
 "surname":"d"
},
{
 "name":"e",
 "surname":"f"
}

I store it in a variable and I would like to have an array of json objects, i.e. a JSON Array .

[{ "name":"a", "surname":"b" }, { "name":"c", "surname":"d" }, { "name":"e", "surname":"f" }]

I would need something as array.push() but I can't do it if I don't split the file before.

I have a problem with a received javascript object. I receive something like this:

{
 "name":"a",
 "surname":"b"
},
{
 "name":"c",
 "surname":"d"
},
{
 "name":"e",
 "surname":"f"
}

I store it in a variable and I would like to have an array of json objects, i.e. a JSON Array .

[{ "name":"a", "surname":"b" }, { "name":"c", "surname":"d" }, { "name":"e", "surname":"f" }]

I would need something as array.push() but I can't do it if I don't split the file before.

Share Improve this question edited Jul 19, 2017 at 9:34 PotatoManager 1,7751 gold badge19 silver badges36 bronze badges asked Jul 19, 2017 at 8:37 VinsMatVinsMat 151 silver badge7 bronze badges 4
  • What is the desired output? I don't really understand what you're trying to do... – Ionut Necula Commented Jul 19, 2017 at 8:38
  • I would like to have an array like this: array[0] = {"name":"a","surname":"b"}; array[1] = {"name":"c","surname":"d"}; – VinsMat Commented Jul 19, 2017 at 8:40
  • I don't understand what is that hard to do...anyway, seems like Yeldar K. gave you the answer. – Ionut Necula Commented Jul 19, 2017 at 8:42
  • if you are receiving just objects as arguments just loop through and add it to your array. If that make sense. – RJ- Commented Jul 19, 2017 at 8:43
Add a ment  | 

2 Answers 2

Reset to default 4

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 mas 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! :)

发布评论

评论列表(0)

  1. 暂无评论