在JavaScript中过滤对象数组?

时间:2019-11-21 18:06:01

标签: javascript

以下程序过滤给定的对象数组,并返回包含满足条件“ living:true”的对象的数组。程序不使用“ if(test(element.living))”,而是使用 “ if(test(element))”,它仍然可以正常工作,这令我感到困惑。那么,为什么程序要测试“ 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}));

2 个答案:

答案 0 :(得分:1)

之所以起作用,是因为在filter中,test是此函数(它以test参数的值接收它):

function(element) {return element.living}

...将返回传入元素的living属性。

那是

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

有效

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

因为所有test所做的都是返回属性的值。

答案 1 :(得分:0)

请勿重新创建Array.prototype.filter函数。 SCRIPTS数组是内置的。您的test(element)函数只是filter已经接受的谓词。

只需拨打电话

SCRIPTS.filter(element => element.living)

以下程序的外部内容:

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; }