在使用没有临时变量的嵌套forEach循环时,如何从main函数返回值?

时间:2017-11-16 18:39:46

标签: javascript arrays ecmascript-6

我想返回primaryNumber,而不必使用额外的变量。

我想要一个只需返回element.number而不需要primaryNumber的箭头语句函数。因为它是内在的forEach是在不同的范围,所以我不能只从element.number返回findPrimaryNumber()。有一个很好的解决方案吗?

function findPrimaryNumber(gender, naturalExpressionYearOfBirth) {
    let primaryNumber, primaryNumberGenderObject;
    if (gender === "f") {
        primaryNumberGenderObject = primaryNumberFemales;
    } else if (gender === "m") {
        primaryNumberGenderObject = primaryNumberMales;
    }

    primaryNumberGenderObject.forEach(element => {
        element.year.forEach(yearWeAreLookingFor => {
            if (yearWeAreLookingFor === naturalExpressionYearOfBirth) {
                primaryNumber = element.number;
            }
        });
    });
    return primaryNumber;
}

以下是它正在搜索的对象的示例:

const primaryNumberMales = [{
    name: "Fire Yin",
    number: 9,
    year: [1910,
      1919,
      1928,
      1937,
      1946,
      1955,
      1964,
      1973,
      1982,
      1991,
      2000,
      2009,
      2018
    ]
  },
  {
    name: "Mountain Yang",
    number: 8,
    year: [1911,
      1920,
      1929,
      1938,
      1947,
      1956,
      1965,
      1974,
      1983,
      1992,
      2001,
      2010,
      2019,
    ]
  },
  {
    name: "Lake Yin",
    number: 7,
    year: [1912,
      1921,
      1930,
      1939,
      1948,
      1957,
      1966,
      1975,
      1984,
      1993,
      2002,
      2011,
      2020,
    ]
  },
  {
    name: "Sky Yang",
    number: 6,
    year: [1913,
      1922,
      1931,
      1940,
      1949,
      1958,
      1967,
      1976,
      1985,
      1994,
      2003,
      2012,
      2021,
    ]
  },
  {
    name: "Earth Core Yang",
    number: 5,
    year: [1914,
      1923,
      1932,
      1941,
      1950,
      1959,
      1968,
      1977,
      1986,
      1995,
      2004,
      2013,
      2022,
    ]
  },
  {
    name: "Wind Yin",
    number: 4,
    year: [1915,
      1924,
      1933,
      1942,
      1951,
      1960,
      1969,
      1978,
      1987,
      1996,
      2005,
      2014,
      2023,
    ]
  },
  {
    name: "Thunder Yang",
    number: 3,
    year: [1916,
      1925,
      1934,
      1943,
      1952,
      1961,
      1970,
      1979,
      1988,
      1997,
      2006,
      2015,
      2024,
    ]
  },
  {
    name: "Earth Yin",
    number: 2,
    year: [1917,
      1926,
      1935,
      1944,
      1953,
      1962,
      1971,
      1980,
      1989,
      1998,
      2007,
      2016,
      2025,
    ]
  },
  {
    name: "Sea Yang",
    number: 1,
    year: [1918,
      1927,
      1936,
      1945,
      1954,
      1963,
      1972,
      1981,
      1990,
      1999,
      2008,
      2017,
      2026,
    ]
  }
];

3 个答案:

答案 0 :(得分:2)

您可以使用find:

   primaryNumberGenderObject.find(element => {
      return element.year.includes(naturalExpressionYearOfBirth)
   }).number;

这将找到回调返回true的第一个元素。

答案 1 :(得分:1)

您可以使用.find

尝试此操作
function findPrimaryNumber(gender, naturalExpressionYearOfBirth) {
    let primaryNumberGenderObject;

    if (gender === "f") {
      primaryNumberGenderObject = primaryNumberFemales;
    } else if (gender === "m") {
      primaryNumberGenderObject = primaryNumberMales;
    }

    return (primaryNumberGenderObject.find(element =>
      element.year.includes(naturalExpressionYearOfBirth)          
    ) || {}).number

    // OR AN INNER FIND, NOT SO ADVISABLE
    // return (primaryNumberGenderObject.find(element =>
    //   element.year.find(yearWeAreLookingFor => 
    //     yearWeAreLookingFor === naturalExpressionYearOfBirth
    //   )
    // ) || {}).number
}

答案 2 :(得分:1)

使用for-loop,您的函数将变得更简单易读,因为您将能够从内循环返回:

function findPrimaryNumber(gender, naturalExpressionYearOfBirth) {
  let primaryNumber, primaryNumberGenderObject;

  if (gender === "f") {
    primaryNumberGenderObject = primaryNumberFemales;
  } else if (gender === "m") {
    primaryNumberGenderObject = primaryNumberMales;
  }

  for (let element of primaryNumberGenderObject) {
    for (let yearWeAreLookingFor of element.year) {
      if (yearWeAreLookingFor === naturalExpressionYearOfBirth) {
        return element.number;
      }
    }
  }
}