如何在我的案例中将数组存储在对象中

时间:2015-06-23 20:18:41

标签: javascript jquery object

我正在尝试将项目推送到数组。

我有类似

的东西
var t = [];
items = [
    {id:123, name:'test1', category:'toy'},
    {id:123, name:'test2', category:'cookies'},
    {id:789, name:'test3', category:'toy'},
    {id:155, name:'test4', category:'pen'},
    {id:155, name:'test5', category:'eraser'}
];

items.map(function(item) {    
    t[item.id].push(item.category);
    //do other things
})

我需要的最终结果

t = [
    {123: ['toy', 'cookies']},
    {789: ['toy']},
    {155: ['pen', 'eraser']}
]

我无法读取未定义的属性'push'。 有没有更好的解决方法?非常感谢!

5 个答案:

答案 0 :(得分:3)

在您的代码中,t是一个数组。在映射函数中,您引用了t中不存在的元素(即t[item.id])。由于t为空,t[item.id]将始终返回undefined。这就是push()不起作用的原因。你得到的错误是试图告诉你这个。您需要先将t[item.id]数组设为push(),然后才能WHERE ( StudentID LIKE 'STUD-[1-9][0-9]' AND StudentID != 'STUD-10' ) OR ( StudentID LIKE 'STUD-[1-9][0-9][0-9]' )

答案 1 :(得分:3)

使用t作为对象而不是数组,每个对象都有一个数组值,也必须先分配,尝试:

var t = {}, items = [
    {id:123, name:'test1', category:'toy'},
    {id:123, name:'test2', category:'cookies'},
    {id:789, name:'test3', category:'toy'},
    {id:155, name:'test4', category:'pen'},
    {id:155, name:'test5', category:'eraser'}
];

items.forEach(function(item){
  !t[item.id] && (t[item.id] = []);
  t[item.id].push(item.category)
})

document.write(JSON.stringify(t));
document.write("<br><b>" + t[123] + "</b>"); //<= Get items by ID

//In your case
var tArray = [];
for(var id in t){
  var obj = {};
  obj[id] = t[id];
  tArray.push(obj)
}
document.write("<br>" + JSON.stringify(tArray));

答案 2 :(得分:0)

如果t包含id,你可以在地图上查看,如果有的话附加一个类别,否则推一个新的obj

    items.map(function(item) {   
    var cid=-1;
    for(i=0;i<t.length;i++){
        if(t[i].id==item.id)
            cid=i;
    }
    var obj={};
    if(cid==-1){
        obj.id=item.id;
        obj.category=[];
        obj.category.push(item.category);
        t.push(obj);  
    }else{
        obj=t[cid];
        obj.category.push(item.category);
        t[cid]=obj;
    }
//do other things
});

答案 3 :(得分:0)

t [item.id]会建议数组t的宽度元素

此代码完成工作:

var t = [];
var j = [];
items = [
    {id:123, name:'test1', category:'toy'},
    {id:123, name:'test2', category:'cookies'},
    {id:789, name:'test3', category:'toy'},
    {id:155, name:'test4', category:'pen'},
    {id:155, name:'test5', category:'eraser'}
];

items.forEach(function(item) {
  var isIn = false;
  j.forEach(function(elt, i) {
    if(item.id == elt)
      isIn = true;
  });
  if(!isIn)
    j.push(item.id);
  isIn = false;
});
j.forEach(function(id) {
  var elts = [];
  items.forEach(function(item) {
    if(item.id == id)
      elts.push(item);
  });
  var obj = {};
  obj.id = id;
  obj.category = [];
  elts.forEach(function(elt) {
    obj.category.push(elt.category);
  });
  t.push(obj);
});
console.log(t);
    //do other things

答案 4 :(得分:0)

首先使用所有数据构建对象,然后将数据形成为所需样式。

&#13;
&#13;
var items = [
        { id: 123, name: 'test1', category: 'toy' },
        { id: 123, name: 'test2', category: 'cookies' },
        { id: 789, name: 'test3', category: 'toy' },
        { id: 155, name: 'test4', category: 'pen' },
        { id: 155, name: 'test5', category: 'eraser' }
    ],
    t = [],
    o,
    i,
    temp = items.reduce(function (res, el) {
        res[el.id] = res[el.id] || [];
        res[el.id].push(el.category);
        return res;
    }, {});
for (i in temp) {
    o = {};
    o[i] = temp[i];
    t.push(o);
}
out(t);
function out(s) {
    document.getElementById('out').innerHTML = '<pre>' + JSON.stringify(s, null, 4) + '</pre>';
}
&#13;
<div id="out"></div>
&#13;
&#13;
&#13;