Tag: arrays

Find the longest word/string in an array

I just started learning JavaScript. I am trying to write a JavaScript to find and print the longest word in an Array. I came up with the code below: var longWords = [“Penelope”, “Johny”, “Aubumayang”, “Czechoslovakia”, “Slovenia”] var longest = 0; for (var i = 0; i < longWords.length; i++) { if (longest < longWords[i].length) […]

Return array to parent function call

I am trying to return an array of objects to a parent function so that I can append them to a div within my HTML. The function that I have appears to be running an incorrect sequence. Here is my function: function getRounds() { var db = connectDB(); var items = new Array(); db.transaction(function(tx) { […]

Removing a specific value in Array

for example, I have this an array of object: var myarray = [{ id: 01, name: alison, active: false}, { id: 04, name: drex, active: true}, { id: 08, name: farel, active: true}, { id: 09, name: dinkoy, active: true}]; I want to delete some of the objects where active == false, or id == […]

Shuffle Array So No Two Keys Are in the Same Location

I’m working on a Secret Santa app, and I’m matching up people by shuffling up an array of users, and then iterating through it using a for loop, then invoking the function if a key in the first array is identical to a key in the second array. However, this is causing some problems when […]

nodeJS util.format passing an array

I am using util.format to format a string like this: util.format(‘My name is %s %s’, [‘John’, ‘Smith’]); The fact that the second parameter is an array: [‘John’, ‘Smith’], prevents my code from replacing the second %s. But i need it to be an array because I don’t know the exact number of the arguments that […]

Find object having maximum value for id in array of javascript objects

Here is my array myArray = [{‘id’:’73’,’foo’:’bar’},{‘id’:’45’,’foo’:’bar’},etc.] I want to find object by id in above array of javascript objects. Generally I use grep() for find in array like this var result = $.grep(myArray, function(e){ return e.id == 73; }); But in this case I need to provide the ID value I want to select.

Removing item from array without iterating

I’ve looked through some documentation, but I feel like there’s an easier method to remove one item from an array without using an iteration loop. http://jsfiddle.net/G97bt/1/ Updated jsFiddle: http://jsfiddle.net/G97bt/6/ using not() <button id=”1″>1</button> <button id=”2″>2</button> <button id=”3″>3</button> <div id=”1a”>Test</div> <div id=”2a”>Test</div> <div id=”3a”>Test</div> var $myList = $(“#1a, #2a, #3a”); $(“#1”).on(“click”, function() { $myList.fadeOut(); // I […]

JavaScript mutating arrays in loop

I want to duplicate certain arrays in arrays using JavaScript, for example: var res = [[“1,2,3”,100,50],[“4,5”,75,10],[“6”,20,90]]; var res2 = []; for(var z in res) { var row = res[z]; var keys = row[0].split(‘,’) for(var y in keys) { var key = keys[y]; res2.push([key,row[1]/keys.length,row[2]/keys.length]); } } /* [ [ ‘1’, 33.333333333333336, 16.666666666666668 ], [ ‘2’, 33.333333333333336, […]

.slice.call vs .push in javascript

One of my friends uses [].slice.call() to fill an array with matched elements. I wonder how this works. I just know the Array.prototype.push to fill an array. Also I have seen there is a difference in both techniques to fill an array. So my questions are: How does Array.prototype.slice help to fill an array? What […]

How to loop over an array and add jQuery click events?

This question already has an answer here: JavaScript closure inside loops – simple practical example 31 answers