How to Iterate Array of Array - javascript

I have an array of array data. I want to loop array of array value and get the first values of each array.
var myArr= [
["1",10],
["2",20],
["3",34]
]
I need to get the first value of each array. Here first value is a string value. How to get "1","2","3" these string value using loop.

to iterate over an array and just do an action with it you can use .forEach
if you would like to iterate over an array and return a new array you can use .map
in this case to just log the first item of each sub array you can
myArr.forEach(x => console.log(x[0])

var myArr= [
["1",10],
["2",20],
["3",34]
]
console.log(myArr.map(item=>item[0]));
You could use map function. This will return an array containing only the first values of each array in the main array.
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Related

Push values to array but as object? JS

I need to push to array of object no array. Check my code..I don;t know where is my problem here
this.trainingPlan?.trainingTemplateExercises.push(this.singleTrainingInfo.trainingExercises.map(obj => {
return obj
}))
in this situation i push array of object. I need to push only object.
Like now i push [{obj...} , {obj2...}]
i need to push only {obj...} , {obj2...}
I need to push only object here..
This is now array like a
[ 0: [ obj1 , obj2]]
Your map here doesn't really do anything. It's just returning the same array you're mapping in the first place. Just concat the two arrays.
this.trainingPlan?.trainingTemplateExercises = this.trainingPlan?.trainingTemplateExercises.concat(this.singleTrainingInfo.trainingExercises)

Pushing simple value into an array on the fly

I want to be able to create an array with only one value inside of it on the fly without using a variable. This works with a variable:
var arr = [];
arr.push('test');
console.log(arr); // correctly logs ["test"]
But this does not:
console.log([].push('test')); // logs 1
Why does this log 1 instead of ["test"]? Is this coerced into a boolean?
Array.prototype.push returns the new length of the array, not the pushed item nor the array it-self.
"I want to be able to create an array with only one value inside of it
on the fly without using a variable"
const arr = ['test'];
console.log(['test']);
Array.prototype.push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
In the first example you are first pushing the element in the array then log the array. But in the second example you are logging the returned result of push().

Put an object into an array in AngularJS

I am using ag-grid for an application and it only takes arrays to display the rows. I have an object returned from the API. This call always only returns 1 object. I want to convert the object into an array of length 1.
This is my code:
jobResult.paged().$promise.then(function (results) {
//ag-grid needs the result to be in an array
var arrayResult = [];
angular.forEach(function(results){
arrayResult.push(results);
});
$scope.jobResult = arrayResult;
if ($scope.lastResultGridOptions.api) {
$scope.lastResultGridOptions.api.setRowData($scope.jobResult);
$scope.lastResultGridOptions.api.sizeColumnsToFit();
}
..rest of function
the results object has the data from the API. But the .foreach function does not push the object into the array.
What am I missing?
Your angular.foreach is wrong,
The correct way is, and then you can take the key or value and push to the array,
angular.forEach(results, function(value, key){
//push key or value
arrayResult.push(value);
});
For your explanation, wanting an array length 1 with the results as [0], why not just push the results into the array:
arrayResult.push(results)
If you are trying to create an array from the results then you would want to run the .forEach on the results:
results.forEach(function(res) { arrayResult.push(res); })

Does .join method change the array into string in Javascript?

The result for .join is different from .push or .pop.
var mack=[];
mack.push("dog", "cat");
mack.join(" ");
alert(mack);
Console.log: ["dog", "cat"]
var mack=[];
mack.push("dog", "cat");
alert(mack.join(" "));
Console.log: "dog cat"
In the first one, mack.join(" "); does not change the original mack array, unlike mack.push("dog", "cat"); or mack.pop();.
I'm curious that why this happened. And is there any other method like this?
The join() function doesn't change the element of array that's only represent the array elements as string with the separator what we give as argument.
here is the reference of join function
Array.prototype.join()
Array push and pop is use to add or remove element from a array that return array new size for push and element for POP.
Array.prototype.push()
Array.prototype.pop()
Here the array elements are ok as you pushed on it.
var mack=[];
mack.push("dog", "cat");
var mack_string = mack.join(" ");
console.log(mack); // Array [ "dog", "cat" ]
console.log(mack_string); // "dog cat"
Push and pop methods are used on Array object to add or remove data to/from Array and therefore change Array itself.
Join method doesn't change Array, it returns String - new object, Array remains unchanged as it is. It could be used e.g. for concatenating Strings which are put in array with particular character.
More about arrays could be found here: http://www.w3schools.com/js/js_array_methods.asp
The join() method joins all elements of an array into a string, detail
It returns a string and does not modify the original array.
Also it does not make sense to assign String for Array object, if that was your expectation.

JQuery - Push method JSONArray

I'm doing the push method between two Arrays to create a bigger Array. The two simple arrays I want to fix are:
[{"id":"11"},{"color":"blue","value":"14"}]
[{"id":"11"},{"color":"green","value":"25"}]
The code to push the two arrays is:
var totjunt = $('body').data('cesta_list').push(array_of_bought_colors_new);
With $('body').data('cesta_list'); I save the first array and then i try to push the second array.
Using console.log(JSON.stringify(totjunt)); I print the value throught the console but the problem is that the console prints only a number 2.
You're logging the result of the push() call, not the resulting array. Try this:
$('body').data('cesta_list').push(array_of_bought_colors_new);
var totjunt = $('body').data('cesta_list');
More specifically, push() returns the length of the new array, not the array itself.
.push doesn't return a new array. It returns the array's new length. The array is updated in-place.

Categories

Resources