Reputation: 165
So on click of the div it is supposed to get the value of its data-type attribute, and look for that in the array. If it isn't in one place it looks at the next object in the array. When it finds it, it brings back the whole object. The issue is I can get it to look through one array with .find. How about multiple? Thanks.
var task1 = { name : "test", test: "nope1" }
var task2 = { name : "test", test: "nope2" }
var task3 = { name : "test", test: "nope3" }
var task4 = { name : "test", test: "nope4" }
var A = [task1, task2];
var B = [task3, task4];
$('.test').click(function(){
var record = $(this).data('type');
var found = /*A,*/B.find(function(t) {
return t.test === record;
});
console.log(found);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test" data-type="nope4">
Nope.
</div>
Upvotes: 0
Views: 65
Reputation: 33726
You can concatenate multiple arrays using the function concat
into one and then execute the function find
.
This example will find the task test: nope3
.
var task1 = {
name: "test",
test: "nope1"
}
var task2 = {
name: "test",
test: "nope2"
}
var task3 = {
name: "test",
test: "nope3"
}
var task4 = {
name: "test",
test: "nope4"
}
var A = [task1, task2];
var B = [task3, task4];
$('.test').click(function() {
var record = $(this).data('type');
var found = A.concat(B).find(function(t) {
return t.test === record;
});
console.log(found);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class='test' data-type='nope3'>Click me!</button>
Upvotes: 3