JavaScript:针对特定类型的鞋子过滤鞋子对象数组,并使用索引作为属性返回对象数组

时间:2019-07-12 18:55:19

标签: javascript arrays function object indexing

我有以下鞋子对象数组:

var currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
];

使用上面的currentInventory数组,我想找到所有系鞋带的鞋并指出哪个单词包含鞋带:

以下所需的输出:

[
  {
    "nameWords": [
      "tasselled",
      "black",
      "low-top",
      "lace-up"
    ],
    "targetWordIndex": 3
  },
  {
    "nameWords": [
      "tasselled",
      "green",
      "low-top",
      "lace-up"
    ],
    "targetWordIndex": 3
  },
  {
    "nameWords": [
      "red",
      "leather",
      "laced",
      "sneakers"
    ],
    "targetWordIndex": 2
  },
  {
    "nameWords": [
      "black",
      "leather",
      "laced",
      "sneakers"
    ],
    "targetWordIndex": 2
  }
]

下面是我尝试的代码;它接近但不完全存在:

function renderLacedShoes(inventory) {

  //console.log(inventory)

  let finalArr = []; 


  for (let i=0; i<inventory.length; i++){
    let indObj = inventory[i];

    let newObj = {};

    // dive into values associated with shoes 

    for (let k=0; k<indObj.shoes.length; k++){
      let shoeNameArr = indObj.shoes[k].name.split(" ");    

      //console.log(shoeNameArr)

      if ( (shoeNameArr.includes('lace-up')) || (shoeNameArr.includes('laced')) ){

        newObj['nameWords'] = shoeNameArr;
        console.log(newObj)

        // not working below
        newObj['targetWordIndex'] = (shoeNameArr.indexOf('lace-up' || 'laced'))
      }
    }
    finalArr.push(newObj);
  }
  return finalArr;
}


// TEST: 
renderLacedShoes(currentInventory)

我的代码输出:

[ { nameWords: [ 'tasselled', 'green', 'low-top', 'lace-up' ],
    targetWordIndex: 3 },
  { nameWords: [ 'black', 'leather', 'laced', 'sneakers' ],
    targetWordIndex: -1 } ]

我看到两个问题:它没有返回所有包含鞋的“鞋带”。另外,我的targetWordIndex已关闭。

我在做什么错?

3 个答案:

答案 0 :(得分:1)

不仅索引有问题,而且推送结果也有问题。这应该在内部循环内部进行。

function renderLacedShoes(inventory) {
    let finalArr = [];
    for (let i = 0; i < inventory.length; i++) {
        let indObj = inventory[i];
        for (let k = 0; k < indObj.shoes.length; k++) {
            let nameWords = indObj.shoes[k].name.split(" ");
            let targetWordIndex = nameWords.findIndex(s => s === 'lace-up' || s === 'laced');
            if (targetWordIndex !== -1) {
                finalArr.push({ nameWords, targetWordIndex });
            }
        }
    }
    return finalArr;
}

var currentInventory = [{ name: 'Brunello Cucinelli', shoes: [{ name: 'tasselled black low-top lace-up', price: 1000 }, { name: 'tasselled green low-top lace-up', price: 1100 }, { name: 'plain beige suede moccasin', price: 950 }, { name: 'plain olive suede moccasin', price: 1050 }] }, { name: 'Gucci', shoes: [{ name: 'red leather laced sneakers', price: 800 }, { name: 'black leather laced sneakers', price: 900 }] }],
    result = renderLacedShoes(currentInventory);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

 let renderLacedShoes = (arr) => {
    const array = [];
    arr.forEach(elem => {
        elem.shoes.forEach(names => {
            let nameWords = names.name.split(" ");
            nameWords.forEach((elem, i) => {
                if (elem.indexOf("lace") >= 0) {
                    array.push({
                        nameWords: nameWords,
                        targetWordIndex: i
                    });
                }
            });

        });
    });
    return array;
}

答案 2 :(得分:0)

您可以尝试使用mapfilter链来达到所需的解决方案

var res = [{ name: 'Brunello Cucinelli', shoes: [{ name: 'tasselled black low-top lace-up', price: 1000 }, { name: 'tasselled green low-top lace-up', price: 1100 }, { name: 'plain beige suede moccasin', price: 950 }, { name: 'plain olive suede moccasin', price: 1050 }] }, { name: 'Gucci', shoes: [{ name: 'red leather laced sneakers', price: 800 }, { name: 'black leather laced sneakers', price: 900 }] }]
 .map(d => d.shoes.map(r => r.name)).flat()
 .filter(d => d.includes('lace'))
 .map(d => ({ nameWords: d.split(' '),  targetWordIndex: d.split(' ').findIndex(d => d.includes('lace'))}))

console.log(res)