-1

array1 is an array of objects, so something like this:

0: {Group: 1, names: "Lastname1, Firstname1", id: 20000001}
1: {Group: 2, names: "Lastname2, Firstname2", id: 20000002}
2: {Group: 3, names: "Lastname3, Firstname3", id: 20000003}

array2 also an array of objects, so something like this:

0: {id: "20000001", photo: ""}
1: {id: "20000001", photo: "/9j/4AAQSkZJRgABAQAAAQABAAD/4QB6RXhpZgAASUkqAAgAAA…9a1EtFKAnd09f/rVlwfdFbVw7LPIoOAGNYwjc/SIaxR//2Q=="}
2: {id: "20000002", photo: ""}
3: {id: "20000003", photo: ""}
4: {id: "20000004", photo: ""}

I want to get the photo value for a specific id based on array1. My array2 can have duplicate values of id, so I want to ignore the one with the empty string.

so at the end of the day, i want to return something like
var image='/9j/4AAQSkZJRgABAQAAAQABAAD/4QB6R...'

encrypt
  • 267
  • 1
  • 3
  • 13
  • 2
    Use array's `filter()`. Read here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter ...Come back to us if you're still having problem with it. – ionizer Dec 01 '18 at 18:59
  • You can loop it and get only if the value is not empty, then exit loop if you are ok with one value – slon Dec 01 '18 at 18:59

5 Answers5

1

You can use find() to get the value you want. It will return the first value found. Empty strings are falser in javascript so you can write the find call back like:

let arr = [
  {id: "20000001", photo: ""},
  {id: "20000001", photo: "/9j/4AAQSkZJRgABAQAAAQABAAD/4QB6RXhpZgAASUkqAAgAAA…9a1EtFKAnd09f/rVlwfdFbVw7LPIoOAGNYwjc/SIaxR//2Q=="},
  {id: "20000002", photo: ""},
  {id: "20000003", photo: ""},
  {id: "20000004", photo: ""}
]

let found = arr.find(item => item.photo && item.id === '20000001')
console.log(found.photo)
Mark
  • 90,562
  • 7
  • 108
  • 148
1

let arr = [{
    id: "20000001",
    photo: ""
  },
  {
    id: "20000001",
    photo: "/9j/4AAQSkZJRgABAQAAAQABAAD/4QB6RXhpZgAASUkqAAgAAA…9a1EtFKAnd09f/rVlwfdFbVw7LPIoOAGNYwjc/SIaxR//2Q=="
  },
  {
    id: "20000002",
    photo: ""
  },
  {
    id: "20000003",
    photo: ""
  },
  {
    id: "20000004",
    photo: ""
  }
]

function findItemByID(arr, id) {
  return arr.find((photoObj) => {
    return photoObj.photo.trim().length !== 0 && photoObj.id === id
  })
}

let photoObj = findItemByID(arr, "20000001");
console.log(photoObj.photo);
Katie.Sun
  • 711
  • 3
  • 15
  • Can u please help me with this, I used your implementation. https://stackoverflow.com/questions/53574769/display-pictures-based-on-specific-student-id – encrypt Dec 01 '18 at 20:29
0
var array2 = [{id: "20000001", photo: ""},
{id: "20000001", photo: "/9j/4AAQSkZJRgABAQAAAQABAAD/4QB6RXhpZgAASUkqAAgAAA…9a1EtFKAnd09f/rVlwfdFbVw7LPIoOAGNYwjc/SIaxR//2Q=="},
{id: "20000002", photo: ""},
{id: "20000003", photo: ""},
{id: "20000004", photo: ""}];

var result = array2.filter(function(a){ return a.photo }).map(a => a.photo);
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
0

Try something like this:

function getForId(photos, id) {
    var tmp = photos.filter(ph => ph.id == id).filter(ph => ph.photo != "");
    if (tmp.length == 0) return undefined;
    return tmp[0];
}

...

x = getForId(array2, "20000001");
Donat
  • 4,157
  • 3
  • 11
  • 26
0

You could map over (via Array.map) the main array and for each item use Array.find to get its photo. Then assign to the current item like this:

const data = [{ Group: 1, names: "Lastname1, Firstname1", id: 20000001 }, { Group: 2, names: "Lastname2, Firstname2", id: 20000002 }, { Group: 3, names: "Lastname3, Firstname3", id: 20000003 } ]
const images = [ {id: "20000001", photo: ""}, {id: "20000001", photo: "/9j/4AAQSkZJRgABAQAAAQABAAD/4QB6RXhpZgAASUkqAAgAAA…9a1EtFKAnd09f/rVlwfdFbVw7LPIoOAGNYwjc/SIaxR//2Q=="}, {id: "20000002", photo: ""} ]

const result = data.map(x => {
  let img = images.find(i => x.id == i.id && i.photo != '') 
  if(img) Object.assign(x, {photo: img.photo})
  return x
})
console.log(result)
Akrion
  • 18,117
  • 1
  • 34
  • 54