Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

I needed a C# Dictionary like data structure in JavaScript and then a way to find that object by a key. I had forgotten how to do this, so did some searching and talked to a colleague and came up with this JsFiddle.

See the code in my jsFiddle or below:

var processingProgressTimeoutIds = [];
var file = {
    name: 'test',
    timeId: 1
};
var file2 = {
    name: 'test2',
    timeId: 2
};
var file3 = {
    name: 'test3',
    timeId: 3
};

processingProgressTimeoutIds.push({
    name: file.name,
    timerId: file.id
});
processingProgressTimeoutIds.push({
    name: file2.name,
    timerId: file2.id
});
processingProgressTimeoutIds.push({
    name: file3.name,
    timerId: file3.id
});

console.log(JSON.stringify(processingProgressTimeoutIds));
// filter will search all the way to the end, it will not break out once it was found
// if you need to stop once it's found, a plain old for loop may be the best choicevar keyName = 'test';
var match = processingProgressTimeoutIds.filter(function (item) {
    return item.name === keyName;
})[0];
console.log(JSON.stringify(match));

// optimization, some will stop when it is found, but it returns a boolean
var match2 = processingProgressTimeoutIds.some(function (element, index, array) {
    return element.name === keyName;
});
console.log(JSON.stringify(match2));

// if you have the full object
var match3 = processingProgressTimeoutIds.indexOf(file);
console.log(JSON.stringify(match3));
// you can use the for loop too
// http://jsperf.com/array-find-equal – from Dave
// indexOf is faster, but I need to find it by the key, so I can’t use it here
//ES6 will rock though, array comprehension! – also from Dave
var ys = [x of xs if x == 3];
var y = ys[0];

// you can also use a for loop and break when it is found
var indexMatch = -1;
for (var i = 0; i < processingProgressTimeoutIds.length; i++) {
    if (processingProgressTimeoutIds[i].name ===keyName) {
        indexMatch = i;
        break;
    }
}
// match
var match4 = processingProgressTimeoutIds[indexMatch];
// if you need to replace it
var replacementFile = { name: 'test4', timeId: 4 }
processingProgressTimeoutIds[indexMatch] = replacementFile;

Here’s a good blog post on Array comprehension.

Search Key Example:

If the search key is totally unique and a string, you can use a property:

var map = { };
map["test"] = // whatever
map["test2"] = // whatever
if("test" in map) {
   var x = map["test"];
}
Posted on Friday, August 22, 2014 6:27 PM JavaScript | Back to top


Comments on this post: Find an element in a JavaScript array

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net