Check if value exists in the array (AngularJS)
You could use indexOf
function.
if(list.indexOf(createItem.artNr) !== -1) {
$scope.message = 'artNr already exists!';
}
More about indexOf:
- http://www.w3schools.com/jsref/jsref_indexof_array.asp
- https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
AngularJs - check if value exists in array object
You could use Array#some
and check with in
operator.
exists = $scope.array.some(function (o) {
return SelectedOptionId in o;
});
AngularJS: How to check if value exists in array of objects?
try this approach:
- create object where keys are name property and total along with applicable are already calculated (
Array.prototype.reduce
) - Iterate over keys of previously created object and transform it back to array (
Object.keys
andArray.prototype.map
)
var res = {};res = Object.keys([ { "name": "Apple", "total": 16, "applicable": 21 }, { "name": "Cherry", "total": 12, "applicable": 27 }, { "name": "Plum", "total": 14, "applicable": 21 }, { "name": "Apple", "total": 16, "applicable": 21 }, { "name": "Cherry", "total": 12, "applicable": 27 }, { "name": "Plum", "total": 14, "applicable": 21 }, { "name": "Banana", "total": 14, "applicable": 21 },].reduce(function (res, item) { if (res[item.name]) { res[item.name].total += item.total; res[item.name].applicable += item.applicable; } else { res[item.name] = item; } return res; }, res)).map(function(key) { return res[key];});console.log(res);
Check if a value exists in an Array object in JavaScript or Angular
You can use Array.prototype.some
var a = [ {id: 1, name: 'foo'}, {id: 2, name: 'bar'}, {id: 3, name: 'test'}];
var isPresent = a.some(function(el){ return el.id === 2});console.log(isPresent);
AngularJS - How to check that a specific key has a specific value in an array
Given colors and cars arrays, you can filter colors by:
var colors =["blue","red","pink","yellow"];var cars=[ {"brand":"Ford","color":"blue"},{"brand":"Ferrari","color":"red"},{"brand":"Rolls","color":"blue"}];
var filteredColors = colors.filter(color => cars.some(car => car.color === color));
console.log(filteredColors);
Related Topics
Reactjs How to Call a Component Function from Another Function on the Same File
Datatables/Tabletools: Format Data as Text When Exporting to Excel
Passing Value from Java to JavaScript
Postman Test - Validating String Values in an Object in an Array
Pass Variable from Android to JavaScript Launched in Webview
How to Hide Blinking Cursor in Input Text
How to Store Multiple Items in Local Storage
Using Setstate to Change Multiple Values Within an Array of Objects - Reactjs
Show Form Fields Based on Selected Option
Recursively Find Keys on an Object
How to Pass Django Variable into JavaScript
How to Know If Browser Tab Is Already Open Using JavaScript
Fix: Js Recursive Function to Get the Nested (Multilevel) Child Objects as Array of Objects
How to Pass Checkbox State to Onclick Function in React
Generate and Download CSV File With PHP and Ajax
How to Retrieve Value in Input Checkbox (Using Reactjs)
What Does Webkitformboundary Mean
React Router Authenticated Route Is Redirecting When I Refresh the Page