从函数调用时,数组未按预期 return

Array does not return as expected when called from a function

我正在尝试遍历嵌套在数组中的嵌套对象数组。
我似乎不能有一个好的 return 值。
抱歉,我是新来的,所以这是我的第一个问题,我希望我的措辞能够让你理解,但我有代码可以显示任何方式。
如果我 console.log(newObjsForShoes) 它按预期工作但如果我 return 它不会并且如果我将它推入一个数组它只有 return 一个值。
下面的代码

function generateLaceDetails(inventory) {

    var arrOfInventory = [];
    var objInInventory = {};
    var arrOfLaceWords = [];
    var newObjsForShoes = {};
    for (var i = 0; i < inventory.length; i++) {
        for (var keys in inventory[i]) {
            if (Array.isArray(inventory[i][keys]) === true) {

                for (var j = 0; j < inventory[i][keys].length; j++) {
                    if (inventory[i][keys][j].name.includes('lace')) {
                        newObjsForShoes['nameWords'] = inventory[i][keys][j].name;

                        newObjsForShoes['nameWords'] = splitToArray(newObjsForShoes);

                        for (var index = 0; index < newObjsForShoes['nameWords'].length; index++) {
                            if (newObjsForShoes['nameWords'][index].includes('lace')) {
                                newObjsForShoes['targetWordIndex'] = index;

                                console.log(newObjsForShoes);
                                arrOfInventory.push(newObjsForShoes)
                                return arrOfInventory;
                            }
                        }
                    }
                }
            }
        }
    }
    // return arrOfInventory;
}

// var array = [];
// array.push(generateLaceDetails(currentInventory))
// console.log(array);

function splitToArray(objs) {
    var arrayOfSplitWords = [];
    for (var key in objs) {
        var splitWords = objs[key].split(" ");
        return splitWords;
    }

}

function assertEquals(actual, expected, testName) {
    actual = JSON.stringify(actual);
    expected = JSON.stringify(expected);
    if (actual === expected) {
        console.log('passed')
    } else {
        console.log('FAILED [' + testName + '] Expected ' + expected + ', but got ' + actual)
    }
}

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
            }
        ]
    }
];
var expectedResult = [{
        "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
    }
];

var actualObj = generateLaceDetails(currentInventory);

assertEquals(actualObj, expectedResult, "generate lace details")

我觉得这个问题让你的生活变得很复杂

const 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} 
  ] } ] 
  
  
function genLaceDetails(inv)
  {
  let res = []
  for (let invEl of inv)
  for (let keyEL in invEl)  
  if (Array.isArray(invEl[keyEL]))
  for (let row of invEl[keyEL])
  if (row.name.includes('lace'))
    {
    let nameWords       = row.name.split(' ')
      , targetWordIndex = nameWords.findIndex(x=>x.includes('lace'))
    res.push({nameWords, targetWordIndex})
    }
  return res
  }

const result = genLaceDetails( currentInventory )

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