Tag: arrays

Get the index of the object inside an array, matching a condition

I have an array like this: [{prop1:”abc”,prop2:”qwe”},{prop1:”bnmb”,prop2:”yutu”},{prop1:”zxvz”,prop2:”qwrq”},…] How can I get the index of the object tha match a condition (without iterate along the array)? For instance: for the prop2==”yutu”, I want to get index 1. I saw the .indexOf() method but think it’s used for simple arrays like: [“a1″,”a2”,…] I checked also the $.grep() […]

JavaScript arrays braces vs brackets

What is the difference between each of the following array definitions. var myArray = []; var myArray = {}; var myArray = new Array();

How to set DOM element as the first child?

I have element E and I’m appending some elements to it. All of a sudden, I find out that the next element should be the first child of E. What’s the trick, how to do it? Method unshift doesn’t work because E is an object, not array. Long way would be to iterate trough E’s […]

How can I remove an element from a list, with lodash?

I have an object that looks like this: var obj = { “objectiveDetailId”: 285, “objectiveId”: 29, “number”: 1, “text”: “x”, “subTopics”: [{ “subTopicId”: 1, “number”: 1 }, { “subTopicId”: 2, “number”: 32 }, { “subTopicId”: 3, “number”: 22 }] } var stToDelete = 2; I have _lodash used in my application for other things. Is […]

Remove duplicates from an array of objects in javascript

I have an object that contains an array of objects. things = new Object(); things.thing = new Array(); things.thing.push({place:”here”,name:”stuff”}); things.thing.push({place:”there”,name:”morestuff”}); things.thing.push({place:”there”,name:”morestuff”}); I’m wondering what is the best method to remove duplicate objects from an array. So for example, things.thing would become… {place:”here”,name:”stuff”}, {place:”there”,name:”morestuff”} Thanks in advance

How to find first element of array matching a boolean condition in JavaScript?

I’m wondering if there’s a known, built-in/elegant way to find the first element of a JS array matching a given condition. A C# equivalent would be List.Find. So far I’ve been using a two-function combo like this: // Returns the first element of an array that satisfies given predicate Array.prototype.findFirst = function (predicateCallback) { if […]

Using jQuery to compare two arrays of Javascript objects

I have two arrays of JavaScript Objects that I’d like to compare to see if they are the same. The objects may not (and most likely will not) be in the same order in each array. Each array shouldn’t have any more than 10 objects. I thought jQuery might have an elegant solution to this […]

Is Chrome's JavaScript console lazy about evaluating arrays?

I’ll start with the code: var s = [“hi”]; console.log(s); s[0] = “bye”; console.log(s); Simple, right? In response to this, Firebug says: [“hi”] [“bye”] Wonderful, but Chrome’s JavaScript console (7.0.517.41 beta) says: [“bye”] [“bye”] Have I done something wrong, or is Chrome’s JavaScript console being exceptionally lazy about evaluating my array?

What is the difference between an array and an object?

The following two different code snippets seem equivalent to me: var myArray = Array(); myArray[‘A’] = “Athens”; myArray[‘B’] = “Berlin”; and var myObject = {‘A’: ‘Athens’, ‘B’:’Berlin’}; because they both behave the same, and also typeof(myArray) == typeof(myObjects) (both yield ‘object‘). Is there any difference between these variants?

In a javascript array, how do I get the last 5 elements, excluding the first element?

[1, 55, 77, 88] // …would return [55, 77, 88] adding additional examples: [1, 55, 77, 88, 99, 22, 33, 44] // …would return [88, 99, 22, 33, 44] [1] // …would return []