Create an array of mixed objects in Javascript - Stack Overflow

I am trying to create an array like this one:["Apple",{1: "Pie", 2: "Dumpling&

I am trying to create an array like this one:

["Apple",{1: "Pie", 2: "Dumpling", 3: "Cider"}, "Banana", {1: "Bread", 2: "Republic"}]

starting from an array of just:

["Apple", "Banana"]

// <input id="Apple"... data-makes="Pie,Dumpling,Cider"> 
// <input id="Banana" ... data-makes="Bread,Republic"

var output = [];
var fruits = ["Apple", "Banana"];
for (i=0; i < fruits.length; ++i)
{
  members = document.getElementById(fruits[i]).getAttribute("data-makes");
  tmp = members.split(",");
  for (k=0; k < tmp.length; ++k)
  {
    output[i][k+1] = tmp[k];
  }
}

Everything I've tried so far is either invalid syntax or makes the initial array undefined.

I am trying to create an array like this one:

["Apple",{1: "Pie", 2: "Dumpling", 3: "Cider"}, "Banana", {1: "Bread", 2: "Republic"}]

starting from an array of just:

["Apple", "Banana"]

// <input id="Apple"... data-makes="Pie,Dumpling,Cider"> 
// <input id="Banana" ... data-makes="Bread,Republic"

var output = [];
var fruits = ["Apple", "Banana"];
for (i=0; i < fruits.length; ++i)
{
  members = document.getElementById(fruits[i]).getAttribute("data-makes");
  tmp = members.split(",");
  for (k=0; k < tmp.length; ++k)
  {
    output[i][k+1] = tmp[k];
  }
}

Everything I've tried so far is either invalid syntax or makes the initial array undefined.

Share Improve this question edited Feb 26, 2016 at 20:37 user1601513 asked Feb 26, 2016 at 20:25 user1601513user1601513 1533 silver badges11 bronze badges 4
  • Can you add your HTML for the elements with data-makes – Andy Commented Feb 26, 2016 at 20:29
  • 1 @Andy also the desired array is 1-D but OP is creating a 2D array output[i][k+1] = tmp[k]; – Ramanlfc Commented Feb 26, 2016 at 20:35
  • Shouldn't the result be more like this: ["Apple",["Pie","Dumpling","Cider"], "Banana", ["Bread","Republic"]] – Alain Nisam Commented Feb 26, 2016 at 20:39
  • it should be more like => {"Apple":["Pie","Dumpling","Cider"],"Banana":["Bread","Republic"]} – Taran J Commented Feb 26, 2016 at 20:41
Add a ment  | 

5 Answers 5

Reset to default 3

You might want to try something like this. You will have to replace the hard coded elements...

var fruits = ["Apple", "Banana"];
var tempString = "Pie,Dumpling,Cider";
var tempArray = new Array();
tempArray.push(tempString);
tempString = "Bread,Republic";
tempArray.push(tempString);
var output = {};
for (var i = 0; i < fruits.length; i++)
{
    var members = tempArray[i].split(",");
    var temp = {};
    for(var k = 0; k < members.length; k++)
    {
        temp[("" + k)] = members[k];
    }
    output[("" + fruits[i])] = temp;
}
console.log(output);
//Different ways to access the objects
console.log(output.Apple);
console.log(output["Apple"]);
console.log(output.Banana[0]);
console.log(output["Banana"]["0"]);

Here's an alternative method you might find useful in the future.

// loop over the fruits
var arr = fruits.reduce(function (fp, fc) {

  // get the string (in your example the data-makes value)
  // and add each to a new object
  var temp = makes[fc].split(', ').reduce(function (p, c, i) {
    p[i + 1] = c;
    return p;
  }, {});

  // then concatenate the fruit name and its object to
  // the output array
  return fp.concat.apply(fp, [fc, temp]);
}, []);

OUTPUT

[
  "Apple", { "1": "pie", "2": "dumpling", "3": "cider"},
  "Banana", { "1": "bread", "2": "Republic" }
]

DEMO

try this:

  var fruits = ["Apple", "Banana"], i, k, members, tmp, output = [];
  var final = [];
    for (i=0; i < fruits.length; ++i)
    {
      members = document.getElementById(fruits[i]).getAttribute("data-makes");
      tmp = members.split(",");
      output[i] = new Object();
      for (k=0; k < tmp.length; ++k)
      {
         output[i][k+1] = tmp[k];
      }
      final.push(fruits[i])
      final.push(output[i])
    }
   console.log(final);

You have two problems in your code:

First, you're never putting fruits[i] in the output array.

Second, you can't assign to output[i][k+1] until you first initialize output[i] to be an array or object.

for (i=0; i < fruits.length; ++i)
{
  output.push(fruits[i]);
  var members = document.getElementById(fruits[i]).getAttribute("data-makes");
  var tmp = members.split(",");
  var obj = {};
  for (k=0; k < tmp.length; ++k)
  {
    obj[k+1] = tmp[k];
  }
  output.push(obj);
}

First, remember that JSON keys are always Strings -- no integers.

["Apple", "Banana"]

// data-makes="Pie,Dumpling,Cider", data-makes="Bread,Republic"
var output = [];
var type;
var fruits = ["Apple", "Banana"];
for (i=0; i < fruits.length; ++i)
{
  members = document.getElementById(fruits[i]).getAttribute("data-makes");
  tmp = members.split(",");
  for (k=0; k < tmp.length; ++k)
  {
    type = {}
    type[fruits[i]] = {}
    type[fruits[i]][k] = tmp[k]
    output.push(type);
  }
}

This will output:

[
 { "apple" : {
               "1" : "pie",
               "2" : "Dumpling",
               "3" : "Cider"
             } 
  },
  { "banana" : {
               "1" : "Bread",
               "2" : "Republic"
             }
]

Not exactly what you asked (I can't imagine the use of a 1-D array for this, but you can get the point)

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

相关推荐

  • Create an array of mixed objects in Javascript - Stack Overflow

    I am trying to create an array like this one:["Apple",{1: "Pie", 2: "Dumpling&

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信