2

I have an array of values that looks like this:

["Ashar", 68345, 14, 46100, "Retail", 1, ""]

I also have an array of objects that looks like this:

[
 {id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, 
 {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, 
 {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, 
 {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, 
 {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}
]

What I am trying to do is get the id in the object from the second array by matching the two arrays. I have written a find function which should in theory find the array of values in the second array so that I can access the id. However, it is returning undefined so something is clearly wrong with what I am doing.

Here is the code I currently have:

let row_id = rows.find(row => {
    dataEntriesArray === row.cells;
});

where rows is the second array and dataEntriesArray is the first one.

Any help in clearing this up for me would be much appreciated.

Thanks for your time.

1
  • 1
    var result = array.filter(function( obj ) { return obj.cells }); Commented May 26, 2016 at 9:42

4 Answers 4

1

This proposal checks every part of the cells array with the search array and returns the id, if found.

function getId(data, search) {
    var id;
    data.some(a => {
        if (a.cells.every((b, i) => b === search[i])) {
            id = a.id;
            return true;
        }
    });
    return id;
}

var search =["Ashar", 68345, 14, 46100, "Retail", 1, ""],
    data=    [{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}];

console.log(getId(data, search));

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for! Thanks for your help!
1

Use Array#every

var ip1 = ["Ashar", 68345, 14, 46100, "Retail", 1, ""];

var ip2 = [{
  id: 1,
  cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]
}, {
  id: 2,
  cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]
}, {
  id: 3,
  cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]
}, {
  id: 4,
  cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]
}, {
  id: 5,
  cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]
}];
var op = ip2.filter(function(item) {
  return item.cells.every(function(el) {
    return ip1.indexOf(el) > -1;
  })
});
console.log(JSON.stringify(op));

Comments

1

try this:

var search =["Ashar", 68345, 14, 46100, "Retail", 1, ""],
foundId = null,
a = [{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}];
search = search.sort().join();
a.forEach(function(entry) {
 if (entry.cells.sort().join() === search){
     foundId = entry.id;
   return;
 }
});

alert(foundId)

JS Fiddle

Comments

1

Short solution using Array.some and JSON.stringify functions:

var item = ["Rob", 94448, 17, 11395, "Retail", 1, ""],
    data = [{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}];

var id;
data.some((obj) => JSON.stringify(obj['cells']) === JSON.stringify(item) && (id = obj['id']), id);

console.log(id);  // 3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.