Joey Marinello Joey Marinello - 4 months ago 18
Javascript Question

JavaScript - Returning index of an array based on matching property from another object

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!

Answer Source

First, your objects JSON is invalid because you are trying to set objects to a property that stores an array, but you didn't wrap the object with { and }. You could just set the variable to a JSON array and then you don't need the property at all (and that's what I'm showing in my answer below).

The Array.filter() method can do this for you without explicit loops.

See below for corrections.

var match_title = { title: "Demo Import - Successful" };

var 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"
    }
]`;

// New object will be stored here
var result = null;

function getObjects(name, data) {

  // parse the JSON into an array and filter the array based on input
  var match = JSON.parse(data).filter(function (entry) {

   // Just check if the item being enumerated has the value you seek
   if (entry.title === name){ 
      // Add the new property and set the placeholder object to the result
      entry.direction = entry.sort[1];
      entry.sort.pop();  // Remove sort direction from property array
      result = entry;
   }
  });
};

// Invoke the function
getObjects(match_title.title, data)
console.log(result);