Posts Check in structured array if exists
Post
Cancel

Check in structured array if exists

1
2
3
4
5
6
7
8
9
10
11
12
// https://stackoverflow.com/a/48538239/1320686

var datas= [
  ["aaa", "bbb"],
  ["ddd", "eee"]
];

function exists(arr, search) {
    return arr.some(row => row.includes(search));
}

console.log(exists(datas, 'ddd'));

On structured (example with property extrasPrices) array as
alt

1
2
3
4
//adding the property name
function exists(arr, search) {
	return arr.some(row => row.extras_name.includes(search));
}

using includes is the same as indexOf, is not the complete string of the array property but a part of.

JS is case sensitive, we dont need it

1
2
3
function exists(arr, search) {
	return arr.some(row => row.extras_name.toLowerCase() == search.toLowerCase() );
}

origin - https://www.pipiscrew.com/?p=16935 check-in-array-if-exists

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags