将一个字符串从一个数组中的一个数组(带有字符串和数字)推送到一个新数组,具体取决于数字的值

我想尽可能多地做自己。首先,我如何从数组中的数组中调用一个元素。那么我如何将一个元素从一个数组中的一个数组推送到一个新数组。最终我想根据年龄组创建新数组。0-20、21-30、31-40、41-50、51-60。打印带有名称和年龄的新数组。


const arr1 = ['Sarah',37];

const arr2 = ['Mike', 32];

const arr3 = ['Bill', 25];

const arr4 = ['Chris', 24];

const arr5 = ['Joe', 44];

const arr6 = ['Jesse', 33];

const arr7 = ['Jon', 28];

const arr8 = ['Michael', 55];

const arr9 = ['Jill', 59];

const arr10 = ['Helia', 4];


const mainArr = [arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10];


const newArr = [];

const age3040 = []


newArr.push(mainArr[[0][0]]);


const checkAge = (ageCheck) => {

    if (arr1[1] > 30 || arr1[1] < 41) {

   age3040.push(arr1[0]);

}

}


console.log(arr1);

console.log(newArr);

console.log(age3040);


慕桂英3389331
浏览 164回答 3
3回答

慕哥9229398

您在这里面临的主要问题是,您无法在迭代列表时创建新的人群,并且被迫对新变量进行硬编码,例如const age3040 = []. 通常,您会使用一个对象来实现这一点,这样您就可以最终得到一个这样的对象。const ranked = {&nbsp; "20": ["Helia"],&nbsp; "30": ["Bill", "Jon"],&nbsp; // and so on}&nbsp;您可能会认为这是一个计数问题。在这种情况下,剩下要做的唯一一件事就是在计算出您需要什么后弄清楚人们属于哪个类别。const ranked = {}for (const [name, age] of mainArr) {&nbsp; // a function that decides what key to give the person in that age bracket&nbsp; const tier = decideTier(age)&nbsp; const people = ranked[tier]&nbsp; if (!people) {&nbsp; &nbsp; // if a previous person with this age has not been seen before you'll&nbsp; &nbsp; // have to create a new array with the person in it so that you can&nbsp; &nbsp; // push new names in it the next time&nbsp; &nbsp; ranked[tier] = [name]&nbsp; } else {&nbsp; &nbsp; people.push(name)&nbsp; }}console.log(ranked["20"])你在这里寻找的东西的一个例子叫做groupBybtw 并且可以在像Ramda这样的库中找到。

拉风的咖菲猫

您的代码中存在三个问题ageCheck&nbsp;根本没有使用||&nbsp;应该&nbsp;&&函数checkAge没有被调用所以你不会得到结果给你举个例子,希望对你有帮助。const arr1 = ['Sarah', 37];const arr2 = ['Mike', 32];const arr3 = ['Bill', 25];const arr4 = ['Chris', 24];const arr5 = ['Joe', 44];const arr6 = ['Jesse', 33];const arr7 = ['Jon', 28];const arr8 = ['Michael', 55];const arr9 = ['Jill', 59];const arr10 = ['Helia', 4];const mainArr = [arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10];const reuslt = [{&nbsp; &nbsp; range: '0-20',&nbsp; &nbsp; names: []}, {&nbsp; &nbsp; range: '21-30',&nbsp; &nbsp; names: []}, {&nbsp; &nbsp; range: '31-40',&nbsp; &nbsp; names: []}, {&nbsp; &nbsp; range: '41-50',&nbsp; &nbsp; names: []}, {&nbsp; &nbsp; range: '51-60',&nbsp; &nbsp; names: []}];for (let index = 0; index < mainArr.length; index++) {&nbsp; &nbsp; const item = mainArr[index];&nbsp; &nbsp; if (item[1] > 0 && item[1] <= 20) {&nbsp; &nbsp; &nbsp; &nbsp; reuslt[0].names.push(item[0]);&nbsp; &nbsp; } else if (item[1] <= 30) {&nbsp; &nbsp; &nbsp; &nbsp; reuslt[1].names.push(item[0]);&nbsp; &nbsp; } else if (item[1] <= 40) {&nbsp; &nbsp; &nbsp; &nbsp; reuslt[2].names.push(item[0]);&nbsp; &nbsp; } else if (item[1] <= 50) {&nbsp; &nbsp; &nbsp; &nbsp; reuslt[3].names.push(item[0]);&nbsp; &nbsp; } else if (item[1] <= 60) {&nbsp; &nbsp; &nbsp; &nbsp; reuslt[4].names.push(item[0]);&nbsp; &nbsp; }}console.log(reuslt);

德玛西亚99

使用 a if else if,你应该使用&&not||像这样:let age020 = []let age2030 = []let age3040 = []let age4050 = []let age5060 = []mainArr.forEach(arr =>{&nbsp; const age = arr[1]if(age >= 0 && age <= 20){&nbsp; &nbsp; age020.push(arr)}else if(age >= 30 && age <= 40){&nbsp; &nbsp; age3040.push(arr)}//... do same to other age range&nbsp;})console.log(age020)console.log(age3040)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript