加入数组键时发生异常

时间:2018-02-12 10:35:55

标签: javascript arrays javascript-objects

我想要例外。我很难解释它,所以让我告诉你我的意思。

我的代码:

fruits = [ 
  {name: "apple",     energy: 100, not: "kiwi"}, 
  {name: "kiwi",      energy: 126, not: "orange"}, 
  {name: "banana",    energy: 150, not: ""}, 
  {name: "orange",    energy: 118, not: "peach"}, 
  {name: "pineapple", energy: 98,  not: ""}, 
  {name: "coconut",   energy: 83,  not: ""}, 
  {name: "peach",     energy: 65,  not: ""}, 
  {name: "lemon",     energy: 36,  not: ""} 
] 

const pick = (arr, n) => {
  const r = [];
  for (var i = 0; i < arr.length; i += n) {
    const slice = arr.slice(i, i+n);
    const name = slice.map(({name}) => name).join('-')
    const energy = slice.reduce((r, {energy}) => r + energy, 0);
    r.push({name, energy})
  }
  return r;
}

const result = pick(fruits, 2)
console.log(result)

我得到的结果是

{name: "apple-kiwi",        energy: 226},
{name: "banana-orange",     energy: 268},
{name: "pineapple-coconut", energy: 181},
{name: "peach-lemon",       energy: 101}

我想要的是异常(not中的fruits)。所以苹果不应该与猕猴桃,猕猴桃和橙子等联系,它应该看起来像这样

{name: "apple-banana",   energy: something},
{name: "kiwi-pineapple", energy: something},
...

我以为我可以过滤slice并检查其中任何项目是否包含not,但我失败了:( 有谁可以帮助我?

2 个答案:

答案 0 :(得分:0)

您需要获取一系列名称和一系列异常,并在交叉点检查它们:

&#13;
&#13;
fruits = [ 
  {name: "apple", energy: 100, not: "kiwi"}, 
  {name: "kiwi", energy: 126, not: "orange"}, 
  {name: "banana", energy: 150, not: ""}, 
  {name: "orange", energy: 118, not: "peach"}, 
  {name: "pineapple", energy: 98, not: ""}, 
  {name: "coconut", energy: 83, not: ""}, 
  {name: "peach", energy: 65, not: ""}, 
  {name: "lemon", energy: 36, not: ""} 
] 

function intersect (a1, a2) {
  var isIntersect = false
  a1.forEach(function (a) {
    if (a.length > 0 && a2.indexOf(a) !== -1) isIntersect = true
  })
  return isIntersect
}

const pick = (arr, n) => {
  const r = [];
  for (var i = 0; i < arr.length; i += n) {
    const slice = arr.slice(i, i+n);
    const namesArr = slice.map(({name}) => name)
    const notsArr = slice.map(({not}) => not)
    
    if (!intersect(namesArr, notsArr)) {
      const name = namesArr.join('-')
      const energy = slice.reduce((r, {energy}) => r + energy, 0);
      r.push({name, energy})
    }
  }
  return r;
}

const result = pick(fruits, 2)
console.log(result)
&#13;
&#13;
&#13;

答案 1 :(得分:0)

实际上,您需要filter数组来选择不具有not属性值的元素,该值等于迭代的item名称:< / p>

const pick = (arr, n) => {
  const r = [];
  for (var i = 0; i < arr.length; i += n) {
    var slice = [];
    slice.push(arr[i]);
    slice = slice.concat(arr.filter(function(x, ind) {
      return x.name !== arr[i].name && x.not !== arr[i].name
    }));
    slice = slice.slice(0, n);
    var name = slice.reduce((r, {
      name
    }) => r + name + "-", "");
    name = name.replace(/\-$/, '');
    const energy = slice.reduce((r, {
      energy
    }) => r + energy, 0);
    r.push({
      name,
      energy
    });
  }
  return r;
}

<强>演示:

&#13;
&#13;
var fruits = [{
    name: "apple",
    energy: 100,
    not: "kiwi"
  },
  {
    name: "kiwi",
    energy: 126,
    not: "orange"
  },
  {
    name: "banana",
    energy: 150,
    not: ""
  },
  {
    name: "orange",
    energy: 118,
    not: "peach"
  },
  {
    name: "pineapple",
    energy: 98,
    not: ""
  },
  {
    name: "coconut",
    energy: 83,
    not: ""
  },
  {
    name: "peach",
    energy: 65,
    not: ""
  },
  {
    name: "lemon",
    energy: 36,
    not: ""
  }
]

const pick = (arr, n) => {
  const r = [];
  for (var i = 0; i < arr.length; i += n) {
    var slice = [];
    slice.push(arr[i]);
    slice = slice.concat(arr.filter(function(x, ind) {
      return x.name !== arr[i].name && x.not !== arr[i].name
    }));
    slice = slice.slice(0, n);
    var name = slice.reduce((r, {
      name
    }) => r + name + "-", "");
    name = name.replace(/\-$/, '');
    const energy = slice.reduce((r, {
      energy
    }) => r + energy, 0);
    r.push({
      name,
      energy
    });
  }
  return r;
}

const result = pick(fruits, 2)
console.log(result)
&#13;
&#13;
&#13;