How to get id from array of Object? - javascript

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

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)

How to access json object using node.js express.js

my json is like this
var jsonObj = JSON.stringify(arr, null, 4);
console.log(jsonObj);
my output like this
I want access each and every element in this json. Plese provide me a solution to do this task
From what I can tell it looks like you have an array of objects and within that object is a sub array ("subActs"). To be able to iterate over all the objects in the "subActs" array you need to iterate over it this way:
jsonObject.forEach(function(object) {
object.subActs.forEach(function(subObject) {
console.log(subObject);
});
});
This will iterate through the entire object and just print to the console the subActs array objects.

Iterate Through array inside object

Using C# regex i have sent modelState object to the java script and add it in the var type temp. Now this object have multiple arrays with in.
One way is to specify the key and get all error msgs from this object like
{{modelState["employee.FirstName"][0]}}
{{modelState["employee.Email"][0]}}
{{modelState[".............."][0]}}
There are almost 8 to 10 key pairs that maybe returns and will take time to write all key value. How do i iterate from this arrays inside object and get all the errors with out specifying the key value.
You could:
var allErrors = [];
$.each( modelState, function( name, errors ){
allErrors = allErrors.concat(errors);
} );

How to stringify an array in Javascript?

I am running the following piece of code:
var arr = [];
arr["aaa"] = {
"xxx" : 1,
"ttt" : 2
};
arr["bbb"] = {
"xxx" : 5,
"qqq" : 6
};
var tmp = JSON.stringify(arr);
alert(tmp);
But the result is []. How does one stringify an array with string keys and object values?
Use
var arr = {};
Arrays should only be used for numerically indexed data, not arbitrary properties. Of course you are able to do it, because they are, in fact, objects. But it won't work in JSON.
Instead, just use objects.
You can't do that, for two reasons:
The stringify method only consider the data in the array, not properties.
There is no JSON format for an array with properties.
If you want the representation of an array in the JSON, you need to put the data in the actual array, not as properties in the array object.
If you want the properties in the JSON, you need to use a plain object instead of an array.

jQuery giving me "undefined" echoing json

I'm trying to print out "message"
JSON:
[{"outcome":false,"message":"This is the message"}]
I've tried 3 things, here are the outcomes of them...
console.log(msg) => [{"outcome":false,"message":"Your account is pending beta activation"}] (this is fine)
var x = $.parseJSON(msg);
console.log(x) gives me an object
console.log(x.message) gives me undefined
What's the correct way to access msg.message, if not like that?
You have an Object in an Array. Use index 0.
x[0].message;
If you're anticipating more Objects in the Array, you can use $.each to iterate the Array.
$.each(x, function(i, obj) {
console.log(obj.message);
});
It looks like your getting a json object containing an array returned, did you try:
console.log(x[0].message);
Your json is an Array of Object's
Instead you should be using
x[0].message;

Categories

Resources