Push values to array but as object? JS - javascript

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)

Related

How to Iterate Array of Array

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

Why an additional array of arrays create inside the JSON object array in javascript

I add JSON objects to an array using push command. After adding that array shows only two JSON objects. Then I add the whole array to another array that contains 7 other arrays. Finally, when I access the JSON object array it shows two JSON objects and one additional array that contain same objects and the array. Here I attached the code and result of the outcome. How can I resolve this?
prevArrhythBeats.push(
{
x: annotationPacket[k].timestamp,
title: annotationPacket[k].annotBeat.a_type,
text: annotationPacket[k].annotBeat.a_desc
}
);
dataFactory.setPrevData(prevChOne, prevChTwo, prevGrid, prevLeadChange, prevMotion, prevArrhythBeats)
dataFactory.setPrevData = function (cOne, cTwo, grid, lead, mot, beat) {
prevData.push(cOne);
prevData.push(cTwo);
prevData.push(grid);
prevData.push(lead);
prevData.push(mot);
prevData.push(beat);
}
before add to dataFactory.setPrevData
After dataFactory.setPrevData method
You are pushing array to itself.

Convert array of array objects into array of objects

I've spent the last couple hours going through some very similar answers to the above question but after a few implementations of loops and reduce I still have not gotten the solution I need.
I am getting an array of objects via service calls. I am pushing those controller array objects to another array because I need a single array to load data into a multi select. I.E.
StatesService.getAreaCities().then(function(response) {
controller.cities = response.data.rows;
controller.areaOptions.push(controller.cities);
});
StatesService.getAreaStates().then(function(response) {
controller.states = response.data.rows;
controller.areaOptions.push(controller.states);
});
controller.areaOptions = [];
This is an example of how I expect and need my array object to look.
With no modification this is how my array looks with those array objects pushed in.
How do I get the above data structure like the 1st image, an array of objects? I tried a solution with reduce()
var newCities = controller.cities.reduce(function(city){
return controller.city;
}, {});
controller.areaOptions.push(newCities);
but it wasnt what I was looking for because it returned a single object with all city property and all its values. I need each object to contain city and its value.
EDIT
I figured out the solution! Thanks to Lex.
I looped over each object in the array in each service and pushed the property.
Thanks to all for steering me in the right direction to figure this out, even the person that downvoted me :)
StatesService.getAreaCities().then(function(response) {
controller.cities = response.data.rows;
controller.cities.forEach(function(city){
controller.areaOptions.push(city);
});
});
Looks like response.data.rows is an array. So when you push response.data.rows into controller.areaOptions you are adding the rows array as an array element. (Basically making it a 2-dimensional array)
You should use Array.prototype.concat

How to get id from array of Object?

I have data in response so here rcsaAssessmentData.data.lobCheckListDTOs is array , riskAsesCklstSessionKey is property of array , How can i get this key using filter or any other option.Below console is printing all objects but i just want value for the riskAsesCklstSessionKey.
So far tried code...
array.js
var opChecklist = rcsaAssessmentData.data.lobCheckListDTOs.filter(function(obj){
return obj.riskAsesCklstSessionKey;
console.log("opcheclist...............",obj.riskAsesCklstSessionKey);
});
$scope.challengesDTO.addChlngToChklst= obj.riskAsesCklstSessionKey;
This is what I think you mean:
You have an array of objects, rcsaAssessmentData.data.lobCheckListDTOs. Each of the objects in the array has a property called riskAsesCklstSessionKey. You are trying to get an array of those keys. If so, try this:
var keys = rcsaAssessmentData.data.lobCheckListDTOs.map(function(a) {return a.riskAsesCklstSessionKey;});

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