对于循环和数组问题 - ngRepeat

时间:2015-02-16 18:14:21

标签: javascript jquery arrays angularjs

我有一个包含一些名为data的数据的数组。

数据如下:



var data = [
  {
    id: 1,
    title: 'title',
    desc: 'desc',
    price: 1.12,
    choice1: 'Color',
    choice2: 'Size',
    large_picture: 'picture',
    choice: 'Gray',
    choice_no: 1
  },
  {
    id: 2,
    title: 'title',
    desc: 'desc',
    price: 1.12,
    choice1: 'Color',
    choice2: 'Size',
    large_picture: 'picture',
    choice: 'one',
    choice_no: 2
  },
  {
    id: 3,
    title: 'title',
    desc: 'desc',
    price: 1.12,
    choice1: 'Color',
    choice2: 'Size',
    large_picture: 'picture',
    choice: 'two',
    choice_no: 2
  },
  {
    id: 4,
    title: 'title',
    desc: 'desc',
    price: 1.12,
    choice1: 'Color',
    choice2: 'Size',
    large_picture: 'picture',
    choice: 'three',
    choice_no: 2
  },
  {
    id: 5,
    title: 'title',
    desc: 'desc',
    price: 1.12,
    choice1: 'Color',
    choice2: 'Size',
    large_picture: 'picture',
    choice: 'four',
    choice_no: 2
  },
  {
    id: 6,
    title: 'title',
    desc: 'desc',
    price: 1.12,
    choice1: 'Color',
    choice2: 'Size',
    large_picture: 'picture',
    choice: 'five',
    choice_no: 2
  },
  {
    id: 7,
    title: 'title',
    desc: 'desc',
    price: 1.12,
    choice1: 'Color',
    choice2: 'Size',
    large_picture: 'picture',
    choice: 'Black',
    choice_no: 1
  },
];




我目前正在尝试遍历data,将它们分成两个独立的数组:colorsize

现在data的对象并不总是choice1choice2作为属性。 (稍后是旁注)

我正在检查第二个选择并按此循环:



if (data[0].choice2) {
  // Has 2 choices
  for (var i in data) {

    if (data[i].choice1.toUpperCase() == 'SIZE' && data[i].choice_no == 1) {
      size[i] = {
        choice: data[i].choice,
        order: data[i].sort
      };
    } else if (data[i].choice2.toUpperCase() == 'SIZE' && data[i].choice_no == 2) {
      size[i] = {
        choice: data[i].choice,
        order: data[i].sort
      };
    } else if (data[i].choice1.toUpperCase() == 'COLOR' && data[i].choice_no == 1) {
      color[i] = {
        choice: data[i].choice,
        order: data[i].sort
      };
    } else if (data[i].choice2.toUpperCase() == 'COLOR' && data[i].choice_no == 2) {
      color[i] = {
        choice: data[i].choice,
        order: data[i].sort
      };
    }

  } // End for()

}




种类有效。 color.length = 7,但它应该只等于2.如果我循环它,如:

for ( var x in color ) {
  console.log(color[x]);  
}

输出:

Object {choice: "Gray", order: 2}
Object {choice: "Black", order: 1}

但是,如果我将循环更改为var x = 0; x < color.length; x++,它会循环显示0-6以及&#34; Gray&#34;之间的所有内容。和&#34;黑&#34;是undefined。现在我只使用第一个循环,因为它的工作原理是&#39;但是ngRepeat与第二个数组类似。它遍历所有7条记录。

TL; DR

我很确定我在if区块的某个地方搞砸了,试图将choice1choice2分成正确的数组(颜色和大小)。 重要要注意choice1 NOT 总是颜色(choice1可能是大小),甚至可能没有任何选择。此外,我对如何修改数据非常有限。

1 个答案:

答案 0 :(得分:2)

而不是使用索引设置数组尝试使用方法.push()

像这样:

if (data[i].choice1.toUpperCase() == 'SIZE' && data[i].choice_no == 1) {
  size.push({ // <--- .push( instead of [i] = 
    choice: data[i].choice,
    order: data[i].sort
  });
} else ...
相关问题