Put an object into an array in AngularJS - javascript

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); })

Related

Defining the data in an array

How can I use the data stored in the array cottageGallery from this mapped data?
const galleryData = components.map(g => {
return {
cottageGallery: g.cottageGallery,
destinationGallery: g.destinationGallery,
activitiesGallery: g.activitiesGallery,
}
})
I thought it would simply be const cottageGallery = galleryData.cottageGallery but this returns undefined.
Not quite, galleryData is going to be an array not an object as you are using javascript's map method. If you wanted to get the first item of the array you could do the following - [0] being the first item of the array.
const cottageGallery = galleryData[0].cottageGallery;
To log each cottageGallery you could use forEach and do the following:
galleryData.forEach(item => {
console.log(item.cottageGallery);
})
galleryData is an array of the objects returned by the map callback. So you'd use galleryData[index].cottageGallery, where index is in the range 0 through galleryData.length - 1 (or any of the various ways you access entries in an array, such as for-of or forEach or...more here).
map Will return an array so you won't be able to simply access galleryData.cottageGallery. You might want to use .reduce() on your array so the outcome can be the object your were trying to create
You can also use forEach to access object array like below:
galleryData.forEach(a => {
console.log(a.cottageGallery);
})

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

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;});

Loop through JSON object returned from cookie

I know there are a bunch of questions out there regarding this, but I have tried several different proposed solutions and get the same results for each one.
I am storing checkbox values in an array in the format timeArr = [ event-name-one: 0: 1404915600, 1: 1404917400 ]. When the array gets updated with a new key/value pair a cookie is updated which stores the array as a JSON object.
I am storing the JSON object in the cookie using jQuery.cookie('day', JSON.stringify(timeArr), {expires: 7}); which stores the array in the following format (returned from console.log();):
{"event-name-one":{"0":1405346400,"1":1405347600},"event-name-two":{"0":1405357200,"1":1405358400}}
In this instance event-name-one and event-name-two are the ID's of the checkboxes. I need to loop through the returned cookie value (JSON object) and check the checkboxes whos ID's are found in the returned cookie.
I have tried a few different loops, i.e. for(var k in cookieValue){} and jQuery.each(jQuery.parseJSON(cookieValue), function(i, v) {}); with no luck.
The for(var k in cookieValue) loop returns each letter of the object separately and the jQuery.each() loop returns this error: Cannot use 'in' operator to search for '76' in ...
How can I convert this JSON string back to an array so I can loop through it and get the 'keys'
jQuery.cookie() must stringify arrays automatically into JSON objects because in order to get the correct results from the loop I had to use jQuery.parseJSON() on the response twice.
Example:
var cookieVal = jQuery.cookie('day_planner');
jQuery.each(jQuery.parseJSON(jQuery.parseJSON(cookieVal)), function(i, v) {
console.log(i, v);
});
This loop returns event-name-one Object {0: "1405346400", 1: "1405347600"}

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