3

I have the following parsed JSON response from an API

emp = 
{
  "response": [
    {
      "image_fingerprint": null,
      "image_source_fingerprint": null,
      "last_name": "LastName",
      "location": "G564",
      "notes": "A great worker",
      "online_seating": {
        "seat_urls": []
      },
      "photo": "/images/employee.jpg",
      "seating": {
        "seated": "not seated",
        "seat_urls": [
                "/api/1/seats/33444",
                "/api/1/seats/55323",
                "/api/1/seats/62229"
            ]
      },
      "show_in_vd": true,
      "start_date": "2014-01-02",
    },

    {
      "image_fingerprint": null,
      "image_source_fingerprint": null,
      "last_name": "LastName",
      "location": "G564",
      "notes": "A great worker",
      "online_seating": {
        "seat_urls": []
      },
      "photo": "/images/employee.jpg",
      "seating": {
        "seated": "not seated",
        "seat_urls": [
                "/api/1/seats/56580",
                "/api/1/seats/69856",
                "/api/1/seats/50003"
            ]
      },
      "show_in_vd": true,
      "start_date": "2014-01-02",
    }
  ]
}

I need to compare the array seat_urls that is inside that response to the following array

shiftA = ["/api/1/seats/50003","/api/1/seats/62229", "/api/1/seats/556565"]

And return all the data from emp if any of the URLs in shiftA matches the URLs in emp.seat_urls

I have tried

var shiftAEmp =  emp.seating.seat_urls.filter(value => shiftA.includes(value))

and many others but I keep getting TypeError: Cannot read property 'seat_urls' of undefined

I tried other filter approaches with no luck it just seam I can't access the seat_urls inside the JSON file emp (sometimes seats_urls cab=n be empty by the way)

Any help will be appreciated

Thanks

3
  • you are missing response. emp.response it will work for you. Commented Jun 9, 2020 at 3:32
  • @sibabratswain I tried adding .response same issue Commented Jun 9, 2020 at 3:55
  • Please check this https://codepen.io/sibabrat_swain/pen/RwrWMeJ check your console. Commented Jun 9, 2020 at 3:59

7 Answers 7

3

Please check the updated emp object below with running code:

var emp = {
  "response": [{
      "image_fingerprint": null,
      "image_source_fingerprint": null,
      "last_name": "LastName",
      "location": "G564",
      "notes": "A great worker",
      "online_seating": {
        "seat_urls": []
      },
      "photo": "/images/employee.jpg",
      "seating": {
        "seated": "not seated",
        "seat_urls": [
          "/api/1/seats/33444",
          "/api/1/seats/55323",
          "/api/1/seats/62229"
        ]
      },
      "show_in_vd": true,
      "start_date": "2014-01-02",
    },
    {
      "image_fingerprint": null,
      "image_source_fingerprint": null,
      "last_name": "LastName",
      "location": "G564",
      "notes": "A great worker",
      "online_seating": {
        "seat_urls": []
      },
      "photo": "/images/employee.jpg",
      "seating": {
        "seated": "not seated",
        "seat_urls": [
          "/api/1/seats/56580",
          "/api/1/seats/69856",
          "/api/1/seats/50003"
        ]
      },
      "show_in_vd": true,
      "start_date": "2014-01-02",
    }
  ]
};

var shiftA = ["/api/1/seats/50003","/api/1/seats/62229", "/api/1/seats/556565"];
var rd = emp.response.filter(r => r.seating.seat_urls.some(u => shiftA.includes(u)));

console.log(rd);

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

1 Comment

the final response should be all of emp.response that matches the seat_urls this is only returning matched seats_url
0

From the looks of things emp is an object that contains an array. You'd need to do something like...

emp.Response[0].seating.....

Or alternatively map the responses

emp.Response.map(myEmp=>{emp.Response[0].seating....})

Comments

0

I think

var shiftAEmp = emp.seating.seat_urls.filter(value => shiftA.includes(value))

is wrong. It should be

emp.Response[0].seating..

emp.Response[1].seating..

Comments

0

SOLUTION:

You are comparing the emp.seating.seat_urls directly, whereas they are arrays, you have to execute it with every item in the array! Try below code!

Get the seat_curls in a variable to simplify the code, and if it works then replace it after!

And bring the whole thing in a function to have proper control over the work!

let shiftA = ["/api/1/seats/50003","/api/1/seats/62229", "/api/1/seats/556565"];
let seatUrls = emp.seating.seat_urls;

function compareSeating(shift1, shiftResponse){
  let shiftAEmp = [];
  shift1.forEach((itemsOfShiftA) => shiftResponse.forEach(itemsOfShiftResponse) => {
     if (itemsOfShiftA === itemsOfShiftResponse) {
        shiftAEmp.push(itemsOfShiftA)
     }
  }
));
return shiftAEmp;
}

Comments

0
var shiftAEmp =  emp.response[0].seating.seat_urls.filter(value => shiftA.includes(value))
//for the first element since its an array
//OR
//for all
var shiftAEmp = emp.response.map(a=>{
  return a.seating.seat_urls.filter(value => shiftA.includes(value))
});

Comments

0

I will show you the solution with each loop. Please follow this.

resultantArray = [];
emp.response.forEach((arr) => {
    if(arr.online_seating.seat_url) {
      resultantArray.push(...arr.online_seating.seat_url)
    }
    if(arr.seating.seat_urls) {
      resultantArray.push(...arr.seating.seat_urls)
    }
});


const intersection = resultantArray.filter(x => shiftA.includes(x));
console.log(intersection);

Comments

0
var shiftAEmp = emp.response.filter(r => {
return r.seating.seat_urls.some(u => shiftA.includes(u));
)

For some method, refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

1 Comment

this answer needs explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.