How can I create a nested array in JavaScript? - Stack Overflow

With the code below:var result = [];result.push({success : 1});for (var i = 0; i < rows.length; i+

With the code below:

var result = [];
result.push({success : 1});
for (var i = 0; i < rows.length; i++) {
    result.push({email : rows[i].email});
};

I create an array that looks like this:

[
  {
    "success": 1
  },
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  }
]

But I want to create an array that looks like this:

[
  {
    "success": 1
  },
  {
    "email": ["[email protected]","[email protected]","[email protected]"]
  }
]

I'm stuck on the exact syntax for doing this. How can I put an array inside another array?

With the code below:

var result = [];
result.push({success : 1});
for (var i = 0; i < rows.length; i++) {
    result.push({email : rows[i].email});
};

I create an array that looks like this:

[
  {
    "success": 1
  },
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  }
]

But I want to create an array that looks like this:

[
  {
    "success": 1
  },
  {
    "email": ["[email protected]","[email protected]","[email protected]"]
  }
]

I'm stuck on the exact syntax for doing this. How can I put an array inside another array?

Share Improve this question edited Jul 14, 2015 at 17:35 brandonscript 73.3k35 gold badges175 silver badges237 bronze badges asked Jul 14, 2015 at 17:14 PietPiet 4152 gold badges8 silver badges18 bronze badges 1
  • 2 as an aside, you aren't creating a "JSONArray". you're creating a JavaScript array that is then being serialized to JSON as it's output to the console. – strugee Commented Jul 14, 2015 at 17:21
Add a ment  | 

4 Answers 4

Reset to default 6
var result = [
  { success : 1 },
  { email   : rows.map(function(row) {
      return row.email;
    })
  }
];

Some explanation: rows is an array, and arrays in JS have a method .map() which can be used to process each item in the array and return a new array with the processed values.

For each item, a function is called with the value of the item, and whichever value is returned from that function is added to the new array.

In this case, the function returns the email property for each of the items, so you end up with an array of e-mail addresses, which is basically what you want.

EDIT: my initial suggestion was to make result an object instead:

var result = {
  success : 1,
  email   : rows.map(function(row) {
    return row.email;
  })
};

Whether this is better depends on the requirements for the structure of the data.

The email property in the result contains an Array, so create one for it.

var result = [];
var emails = [];

for (var i = 0; i < rows.length; i++) {
   emails.push(rows[i].email);
};

result.push({success : 1});
result.push({email: emails});

Simply create an array in the result object with the property email and then push the emails into it. See below.

var result = [];
result.push({success : 1});
var emails = []
for (var i = 0; i < rows.length; i++) {
    emails.push(rows[i].email);
};
result.push({email: emails})
var finalResult = [];    
finalResult.push({success : 1});
var result = [];
for (var i = 0; i < rows.length; i++) {
    result.push(email : rows[i].email);
};

finalResult.push({"email" : result});

i think above code will work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477482a4629410.html

相关推荐

  • How can I create a nested array in JavaScript? - Stack Overflow

    With the code below:var result = [];result.push({success : 1});for (var i = 0; i < rows.length; i+

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信