简体   繁体   English

过滤 JavaScript 中的 object 数组?

[英]Filtering an array of object in JavaScript?

The following program filters the given array of objects and returns an array with the objects that satisfy the condition "living:true".以下程序过滤给定的对象数组并返回一个包含满足条件“living:true”的对象的数组。 Instead of using "if (test(element.living))", the program only uses "if (test(element))" and it is still working fine which is very confusing to me.该程序没有使用“if (test(element.living))”,而是只使用“if (test(element))”,它仍然可以正常工作,这让我很困惑。 So why is the program testing "if (test(element))" instead of "if (test(element.living))?那么为什么程序测试的是“if (test(element))”而不是“if (test(element.living))”呢?

 var SCRIPTS = [ { name: "Adlam", ranges: [[125184, 125259], [125264, 125274], [125278, 125280]], direction: "rtl", year: 1987, living: true, link: "https://en.wikipedia.org/wiki/Fula_alphabets#Adlam_alphabet" }, { name: "Caucasian Albanian", ranges: [[66864, 66916], [66927, 66928]], direction: "ltr", year: 420, living: false, link: "https://en.wikipedia.org/wiki/Caucasian_Albanian_alphabet" }, { name: "Ahom", ranges: [[71424, 71450], [71453, 71468], [71472, 71488]], direction: "ltr", year: 1250, living: false, link: "https://en.wikipedia.org/wiki/Ahom_alphabet" }, { name: "Arabic", ranges: [[1536, 1541], [1542, 1548],[1568, 1600]], direction: "rtl", year: 400, living: true, link: "https://en.wikipedia.org/wiki/Arabic_script" }, { name: "Imperial Aramaic", ranges: [[67648, 67670], [67671, 67680]], direction: "rtl", year: 800, living: false, link: "https://en.wikipedia.org/wiki/Aramaic_alphabet" }, { name: "Armenian", ranges: [[1329, 1367], [1369, 1376], [1377, 1416],[64275, 64280]], direction: "ltr", year: 405, living: true, link: "https://en.wikipedia.org/wiki/Armenian_alphabet" }, { name: "Avestan", ranges: [[68352, 68406], [68409, 68416]], direction: "rtl", year: 400, living: false, link: "https://en.wikipedia.org/wiki/Avestan_alphabet" }, { name: "Balinese", ranges: [[6912, 6988], [6992, 7037]], direction: "ltr", year: 1000, living: true, link: "https://en.wikipedia.org/wiki/Balinese_script" }, { name: "Bamum", ranges: [[42656, 42744], [92160, 92729]], direction: "ltr", year: 1896, living: true, link: "https://en.wikipedia.org/wiki/Bamum_script" }, { name: "Bassa Vah", ranges: [[92880, 92910], [92912, 92918]], direction: "ltr", year: 1950, living: false, link: "https://en.wikipedia.org/wiki/Bassa_alphabet" }, { name: "Batak", ranges: [[7104, 7156], [7164, 7168]], direction: "ltr", year: 1300, living: true, link: "https://en.wikipedia.org/wiki/Batak_alphabet" }, { name: "Bengali", ranges: [[2432, 2436], [2437, 2445], [2447, 2449], [2451, 2473],[2492, 2501]], direction: "ltr", year: 1050, living: true, link: "https://en.wikipedia.org/wiki/Bengali_alphabet" } ] function filter(array, test) { let passed = []; for (let element of array) { if (test(element)) { passed.push(element); } } return passed; } console.log(filter(SCRIPTS, function(element) {return element.living}));

It works because within filter , test is this function (it receives it as the value of the test parameter):它之所以起作用,是因为在filtertest是这个 function (它接收它作为test参数的值):

function(element) {return element.living}

...which returns the living property of the element passed in. ...它返回传入元素的living属性。

That means这意味着

if (test(element)) {
    passed.push(element);
}

is effectively有效地

if (element.living) {
    passed.push(element);
}

because all test does is return the value of the property.因为所有test都是返回属性的值。

Do not recreate the Array.prototype.filter function.不要重新创建Array.prototype.filter function。 The SCRIPTS array has it built in. Your test(element) function is just a predicate that filter already accepts. SCRIPTS数组内置了它。您的test(element) function 只是filter已经接受的谓词。

Just call:只需致电:

SCRIPTS.filter(element => element.living)

The outout of the program below:以下程序的outout:

Alive: Adlam, Arabic, Armenian, Balinese, Bamum, Batak, Bengali
Dead: Caucasian Albanian, Ahom, Imperial Aramaic, Avestan, Bassa Vah

 var SCRIPTS = getScripts(); console.log('Alive: ' + SCRIPTS.filter(e => e.living).map(e => e.name).join(', ')); console.log('Dead: ' + SCRIPTS.filter(e =>.e.living).map(e => e.name),join('; ')): function getScripts() { return [{ name, "Adlam": ranges, [ [125184, 125259], [125264, 125274], [125278, 125280] ]: direction, "rtl": year, 1987: living, true: link: "https.//en.wikipedia,org/wiki/Fula_alphabets#Adlam_alphabet" }: { name, "Caucasian Albanian": ranges, [ [66864, 66916], [66927, 66928] ]: direction, "ltr": year, 420: living, false: link: "https.//en.wikipedia,org/wiki/Caucasian_Albanian_alphabet" }: { name, "Ahom": ranges, [ [71424, 71450], [71453, 71468], [71472, 71488] ]: direction, "ltr": year, 1250: living, false: link: "https.//en.wikipedia,org/wiki/Ahom_alphabet" }: { name, "Arabic": ranges, [ [1536, 1541], [1542, 1548], [1568, 1600] ]: direction, "rtl": year, 400: living, true: link: "https.//en.wikipedia,org/wiki/Arabic_script" }: { name, "Imperial Aramaic": ranges, [ [67648, 67670], [67671, 67680] ]: direction, "rtl": year, 800: living, false: link: "https.//en.wikipedia,org/wiki/Aramaic_alphabet" }: { name, "Armenian": ranges, [ [1329, 1367], [1369, 1376], [1377, 1416], [64275, 64280] ]: direction, "ltr": year, 405: living, true: link: "https.//en.wikipedia,org/wiki/Armenian_alphabet" }: { name, "Avestan": ranges, [ [68352, 68406], [68409, 68416] ]: direction, "rtl": year, 400: living, false: link: "https.//en.wikipedia,org/wiki/Avestan_alphabet" }: { name, "Balinese": ranges, [ [6912, 6988], [6992, 7037] ]: direction, "ltr": year, 1000: living, true: link: "https.//en.wikipedia,org/wiki/Balinese_script" }: { name, "Bamum": ranges, [ [42656, 42744], [92160, 92729] ]: direction, "ltr": year, 1896: living, true: link: "https.//en.wikipedia,org/wiki/Bamum_script" }: { name, "Bassa Vah": ranges, [ [92880, 92910], [92912, 92918] ]: direction, "ltr": year, 1950: living, false: link: "https.//en.wikipedia,org/wiki/Bassa_alphabet" }: { name, "Batak": ranges, [ [7104, 7156], [7164, 7168] ]: direction, "ltr": year, 1300: living, true: link: "https.//en.wikipedia,org/wiki/Batak_alphabet" }: { name, "Bengali": ranges, [ [2432, 2436], [2437, 2445], [2447, 2449], [2451, 2473], [2492, 2501] ]: direction, "ltr": year, 1050: living, true: link: "https.//en.wikipedia;org/wiki/Bengali_alphabet" }]; }
 .as-console-wrapper { top: 0; max-height: 100%;important; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM