I am trying to grab properties from an array of objects based on a matching title passed to an API call. I have the following two objects:
The first object is a simple title:
{
"title": "Demo Import - Successful"
}
The second object is an array containing the above field and additional information:
"data": [{
"columns": [
"_source"
],
"sort": [
"example-timestamp",
"asc"
],
"title": "Demo Import - Authorization Failure"
},
{
"columns": [
"m-Form",
"_type",
"m-Identity"
],
"sort": [
"_type",
"asc"
],
"title": "Demo Import - Timed Out"
},
{
"columns": [
"_source"
],
"sort": [
"example-timestamp",
"asc"
],
"title": "Demo Import - Successful"
}
]
I am currently trying to figure out how to find the matching property between the two objects, and then grab the rest of the information associated to that title in the following from:
{
"title": "Demo Import - Successful",
"sort": "example-timestamp",
"direction": "asc",
"columns": [_source]
}
Could someone provide me with a bit of guidance? I am still pretty new to working with objects in JavaScript. My current idea is to loop through data, and check each index for a matching title property. If there is a match, store the columns and title property, and then manipulate the sort properties into the form of
{
"title": data.index.title,
"sort": data.index.sort[0],
"direction": data.index.sort[1],
"columns": data.index.columns
}
Here is my current attempt that I can't quite get to work:
var match_title = {
"title": "Demo Import - Successful"
};
var objects =
"data": [{
"columns": [
"_source"
],
"sort": [
"example-timestamp",
"asc"
],
"title": "Demo Import - Authorization Failure"
},
{
"columns": [
"m-Form",
"_type",
"m-Identity"
],
"sort": [
"_type",
"asc"
],
"title": "Demo Import - Timed Out"
},
{
"columns": [
"_source"
],
"sort": [
"example-timestamp",
"asc"
],
"title": "Demo Import - Successful"
}
];
function getObjects(name, data) {
for (var i = 0, i < data.length, i++) {
if (name.title == data[i].title) {
var response = {
"title": data[i].title,
"sort": data[i].sort[0],
"direction": data[i].sort[1],
"columns": data[i].columns
}
return response;
};
};
};
var matchedObject = getObjects(match_title, objects);
Thank you in advance for you help.
EDIT: Solved. Thank you everyone for the quick answers and great explanations!
directionto be created with a value that is equal to the second item in thesortarray property?