简体   繁体   English

如何在多维数组中返回 boolean 之后的值?

[英]How to return values after boolean in multidemensional array?

Im trying to filter a multidimensional array to return only the indexes after a boolean in this array.我试图过滤多维数组以仅返回此数组中 boolean 之后的索引。

The first key in this array needs to stay in the array like "Tafelblad model" and "Uitstraling" And the optional values after the booleans该数组中的第一个键需要像“Tafeblad model”和“Uitstraling”一样留在数组中,以及布尔值后面的可选值

["Tafelblad model", true, "Rechthoekig", "Vierkant", "Ovaal / Rond / Overig"],
["Uitstraling", "Licht robuust", "Midden robuust", true, "Zwaar robuust", "Weinig", "Midden (standaard)", true, "Veel oneffenheden en scheuren"]`

The new array would be like新数组就像

["Tafelblad model", "Rechthoekig"],
["Uitstraling", "Zwaar robuust", "Veel oneffenheden en scheuren"]`

You can close over a flag that you set to keep the next entry you see:您可以关闭您设置的标志以保留您看到的下一个条目:

let flag = true; // We want the first one
const newArray = array.filter(entry => {
    const keep = flag;
    flag = typeof entry === "boolean";
    return keep;
});

Live Example:现场示例:

 function example(array) { let flag = true; // We want the first one const newArray = array.filter(entry => { const keep = flag; flag = typeof entry === "boolean"; return keep; }); console.log(newArray); } example(["Tafelblad model", true, "Rechthoekig", "Vierkant", "Ovaal / Rond / Overig"]); example(["Uitstraling", "Licht robuust", "Midden robuust", true, "Zwaar robuust", "Weinig", "Midden (standaard)", true, "Veel oneffenheden en scheuren"]);

You can use the .filter(..) function for that, here is an example:您可以为此使用.filter(..) function,这是一个示例:

 let arr1 = ["Tafelblad model", true, "Rechthoekig", "Vierkant", "Ovaal / Rond / Overig"]; let arr2 = ["Uitstraling", "Licht robuust", "Midden robuust", true, "Zwaar robuust", "Weinig", "Midden (standaard)", true, "Veel oneffenheden en scheuren"]; const filterFn = (value, index, arr) => { return index === 0 || arr[index - 1] === true; }; arr1 = arr1.filter(filterFn); arr2 = arr2.filter(filterFn); console.log(arr1); console.log(arr2);

The callback function passed to .filter(..) receives the value at the current iteration, the current iteration's index and the original array as parameters.传递给.filter(..)的回调 function 接收当前迭代的值、当前迭代的索引和原始数组作为参数。 The conditions in the filterFn function check if the iteration's index is 0 (the value is the first value in the array) or the previous item is strict equal to true filterFn function 中的条件检查迭代的索引是否为0 (该值是数组中的第一个值)或前一项严格等于true

Using map() and filter()使用map()filter()

 let arrays = [ ["Tafelblad model", true, "Rechthoekig", "Vierkant", "Ovaal / Rond / Overig"], ["Uitstraling", "Licht robuust", "Midden robuust", true, "Zwaar robuust", "Weinig", "Midden (standaard)", true, "Veel oneffenheden en scheuren"] ] let result = arrays.map(arr => { return arr.filter((v, i) => typeof arr[i - 1] === "boolean" || i == 0); }) console.log(result)

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

相关问题 向多维数组添加值 - add values to multidemensional array 如果数组索引值等于 ax & y 值,如何返回 boolean - How to return a boolean if array index values are equal to a x & y values map function 返回 1 boolean 值,而不是 Z84E2C64F38F78BA3EA5C905DA27A 值的数组 - map function to return 1 boolean value, instead of an array of boolean values 如何使用循环将多维数组填充到 html 表中 - How to populate a multidemensional array into an html table using a loop 从数组中返回“false”布尔值的数量 - MobX & React - Return number of "false" Boolean values from an array - MobX & React 如果两个对象数组具有值,则比较并返回 boolean - Compare and return boolean if two array of objects has the values or not 数组中的未定义条目在通过`every`解析为布尔值后返回`true`。 - Undefined entries in an array return `true` after parsing them as boolean with `every` 如何计算布尔值数组中真值的个数? - How to count the number of trues in an array of boolean values? JavaScript中的布尔值数组 - Array of boolean values in JavaScript 如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回 boolean true 停止使用 Javascript 检查 - How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM