Reputation: 10228
I have these two arrays:
var Names = ['jack', 'peter', 'jack', 'john'];
var Ids = ['1' , '2' , '3' , '4' ];
Also I have this variable:
var name = 'ja'; // to search in the Names array
I search name
variable in the Names
array as follow:
var MatchesNames = Names.filter(function(x){
if(x.indexOf(name) >= 0) return x
}).slice(0,4); // slice() makes limit the results
The output of above code is:
alert(MatchesNames);
//=> jack,jack
As you see there is two duplicate names (which have different ids) .. Now I need to pull their ids out from Ids
array. How can I do that? I guess I need an object but really I don't know how can I use an object in this case.
I want this output:
//=> jack(1),jack(3)
How can I do that?
Upvotes: 0
Views: 87
Reputation: 31
You may want something like this.
var Names = [{name: 'jack', id: 1}, {name: 'peter', id: 2}, {name: 'jack', id: 3}, {name: 'john', id: 4}];
var chars = 'ja';
var MatchesNames = Names.filter(function(x) {
if(x.name.indexOf(chars) >= 0) {
return x;
};
}).slice(0,4);
MatchesNames.forEach(function(person) {
console.log(person.name + '(' + person.id + ')');
});
Upvotes: 1
Reputation: 14423
I'd use reduce
:
var Names = ['jack', 'peter', 'jack', 'john'];
var Ids = ['1', '2', '3', '4'];
var search = name => Names.reduce((r, n, i) =>
n.indexOf(name) > -1 ? (r.push([n, Ids[i]]), r) : r, []);
results.innerHTML = JSON.stringify(search('ja'));
<pre id="results"></pre>
Upvotes: 1
Reputation: 118271
You can use the Array index as id, and process it like:
var names = ['jack', 'peter', 'jack', 'john'];
var ids = ['1' , '2' , '3' , '4' ];
var name = 'ja';
var result = [];
var zippedArray = names.map(function (e, i) {
return [names[i], ids[i]];
});
zippedArray.map(function(element, index) {
if(element[0].indexOf(name) >= 0) {
result.push( element[0] + "(" + element[1] + ")" );
}
});
console.log(result.toString());
// jack(1), jack(3)
Upvotes: 2
Reputation: 6564
I am not a professional but this may help. By the way you'll have to make some retouches.
var Names = ['jack', 'peter', 'jack', 'john'];
var Ids = ['1' , '2' , '3' , '4' ];
function fakeZip(arr1, arr2) {
var ret = new Object;
for (var i=0; i<arr1.length; i++) {
ret[arr2[i]] = arr1[i];
}
return ret;
}
var newobj = fakeZip(Names, Ids);
JSON.stringify(newobj);
/*
{"1":"jack","2":"peter","3":"jack","4":"john"}
*/
function lookFor(el, obj) {
res = []
for(key in obj) {
if(obj[key]== el ){
res.push(key)
}
}
return [el, res];
}
lookFor("jack", newobj)
/*
jack,1,3
*/
Upvotes: 0
Reputation: 28611
Check the documentation for .filter
, there's an option to call with the array index.
(It also returns true/false, not the actual value, so current usage is wrong, but a side issue).
This way, you can get an array of the IDs that match, at the time they match:
var Names = ['jack', 'peter', 'jack', 'john'];
var Ids = ['1' , '2' , '3' , '4' ];
var name = 'ja';
var MatchesIds = [];
var MatchesNames = Names.filter(function(x, i){
console.log("Check: " + i + " : " + x)
if(x.indexOf(name) >= 0) {
MatchesIds.push(i)
return true
}
})
console.log(MatchesNames)
console.log(MatchesIds)
Example fiddle: https://jsfiddle.net/nkuntaup/
Upvotes: 1
Reputation: 386560
Approach with an Array#forEach()
and a result array with objects.
function find(o, s) {
var r = [];
o.names.forEach(function (a, i) {
if (~a.indexOf(s)) {
r.push({ id: o.ids[i], name: a });
}
});
return r;
}
var Names = ['jack', 'peter', 'jack', 'john'],
Ids = ['1', '2', '3', '4'],
result = find({ ids: Ids, names: Names }, 'ja');
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
document.write(result.map(function (a) { return a.name + '(' + a.id + ')'; }).join(','));
Upvotes: 1
Reputation: 1390
If you want to associate the name with the id, you should really use an object; otherwise you will have to assume that the id array and names are in the correct order, but it would be difficult to guarantee.
var people = [
{ name: 'jack', id: 1 },
{ name: 'peter', id: 2 },
{ name: 'jack', id: 3 },
{ name: 'john', id: 4 }
];
var name = 'ja';
var matchedPeople = people.filter((person) => person.name.includes(name));
var matchedIds = matchedPeople.map((person) => person.id);
You could also incorporate Ramda/Lodash to compose these functions
Upvotes: 1