【问题标题】:Best way to optimize this function优化此功能的最佳方法
【发布时间】:2020-10-01 04:33:08
【问题描述】:

我有一个函数如下,它遍历大量的 json 数据。

const findMakeAndModel = (make, model) => {
  let foundMakeModel = false;
  makeModel.map(value => {
    if (value.makeCode === make.toUpperCase()) {
      value.model.map(makeModel => {
          if (makeModel.modelValue === `${make} - ${model}`) {
            foundMakeModel = true;
          }
      })
    }
  });
  return foundMakeModel
}

虽然它有效,但我希望时间减少它所花费的时间。我试图在if (makeModel.modelValue === ${make} - ${model}) 条件内返回,但不知何故它不起作用。任何帮助表示赞赏。

上面的 json 是我正在使用的,但实际的要大得多。这就是为什么遍历需要时间。

【问题讨论】:

  • 如果您不使用返回值,请不要调用map()。它创建一个新列表。如果您只想循环查看副作用,请使用forEach()
  • 正如@Barmar 提到的,使用forEach() 进行迭代。还要将make.toUpperCase()${make} - ${model} 移动到循环上方,以避免在每次迭代中调用。

标签: javascript json ecmascript-6


【解决方案1】:

无法在中间停止 mapforEach 循环。如果要在找到满足条件的元素后停止循环,则需要使用for 循环。

或者您可以使用find 方法。这将返回满足条件的第一个元素,因此一旦找到它将停止搜索。然后,您可以检查 this 是否不为 null 并返回它。

另外,你的变量不是一个数组,它是一个对象。您需要使用data.makeModel 来获取包含数组的属性。

const findMakeAndModel = (make, model) => {
  let makeUpper = make.toUpperCase(); // don't repeat this every time
  let makeModelCombined = `${make} - ${model}`; // or this
  let foundMakeModel =
    data.makeModel.find(value => value.makeCode === makeUpper &&
      value.model.find(makeModel => makeModel.modelValue === makeModelCombined))
  return foundMakeModel !== null;
}

let data = {
    "makeModel": [{
            "makeId": 1,
            "makeCode": "NISSAN",
            "makeValue": "Nissan",
            "model": [{
                    "modelCode": "NIS032",
                    "modelValue": "Nissan - Cube"
                },
                {
                    "modelCode": "NIS012",
                    "modelValue": "Nissan - March"
                },
                {
                    "modelCode": "NIS033",
                    "modelValue": "Nissan - Note CVT"
                }
            ]
        },
        {
            "makeId": 2,
            "makeCode": "OPEL",
            "makeValue": "Opel",
            "model": [{
                    "modelCode": "OPE005",
                    "modelValue": "Opel - Zafira"
                },
                {
                    "modelCode": "OPE009",
                    "modelValue": "Opel - Astra"
                }
            ]
        },
        {
            "makeId": 21,
            "makeCode": "DAIHATSU",
            "makeValue": "Daihatsu",
            "model": [{
                    "modelCode": "DAI006",
                    "modelValue": "Daihatsu - Terios 7"
                },
                {
                    "modelCode": "DAI015",
                    "modelValue": "Daihatsu - Extol"
                },
                {
                    "modelCode": "DAI007",
                    "modelValue": "Daihatsu - YRV Turbo"
                },
                {
                    "modelCode": "DAI010",
                    "modelValue": "Daihatsu - M303RS"
                }
            ]
        },
        {
            "makeId": 31,
            "makeCode": "MITSUBISHI",
            "makeValue": "Mitsubishi",
            "model": [
                {
                    "modelCode": "MIT042",
                    "modelValue": "Mitsubishi - Lancer Evo VIII"
                },
                {
                    "modelCode": "MIT009",
                    "modelValue": "Mitsubishi - i"
                }
            ]
        },
    ]
};

console.log(findMakeAndModel('Nissan', 'March'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2014-07-19
    • 1970-01-01
    • 2011-02-19
    • 2014-10-17
    • 1970-01-01
    • 2018-02-10
    相关资源
    最近更新 更多