Fred
Fred

Reputation: 212

Add elements to an array, according to the value of another array

I have two arrays:

  1. video, brings objects with the following elements: id, title and url.
  2. watched_videos, brings objects with the following elements: watched, video_id.

I need to check which objects in the video array have an id equal to video_id, all that satisfy this condition, add a new element called watched, with a value of true, those that do not, add a watched, with a value of false.

I know I can do this using the filter and include functions to manipulate the arrays, however, I can't understand a way that works.

I'm learning about handling arrays.

Edit: @Prerak Sola I stopped here, when I tried to check if there is an identical id in watched_videos.

const module_videos = videos.filter((v) =>
  v.id.include(watched_videos.video_id)
);

I'm getting the error: "v.id.include is not a function",

Edit 2:

I need to receive the following result:

videos = [{
    "id": 1,
    "title": "one",
    "url": "/v1",
    "watched": true
  }, {
    "id": 2,
    "title": "two",
    "url": "/v2"
    "watched": false
  }
];

Upvotes: 0

Views: 57

Answers (3)

user3595026
user3595026

Reputation: 629

var videos = [{
    "id": 1,
    "title": "one",
    "url": "/v1"
  }, {
    "id": 2,
    "title": "two",
    "url": "/v2"
  }, {
    "id": 3,
    "title": "three",
    "url": "/v3"
  }, {
    "id": 4,
    "title": "four",
    "url": "/v4"
  }

];

var watched_videos = [

  {
    "video_id": 7
  }, {
    "video_id": 2
  }, {
    "video_id": 3
  }, {
    "video_id": 8
  }

];

watched_videos.map(watchedVideo => {
  let filtered = videos.filter(video => watchedVideo.video_id === video.id);

  
    return watchedVideo.watched = filtered.length > 0;


})

console.log(watched_videos)

Upvotes: 1

D. Seah
D. Seah

Reputation: 4592

you can create a lookup map watched video id

const videos = [{
  "id": 1,
  "title": "one",
  "url": "/v1",
}, {
  "id": 2,
  "title": "two",
  "url": "/v2",
}];

const watched = [{
  watched: true,
  video_id: 1
}, {
  watched: false,
  video_id: 2
}, ]

const watched_video = watched.filter(x => x.watched).map(x => x.video_id);

videos.forEach(v => {
    v.watched = watched_video.includes(v.id);
});

console.log(videos);

Upvotes: 0

Cláudio Silva
Cláudio Silva

Reputation: 10

I would do like that

const video = [
  {
    id: 12,
    title: 'test1',
    url: 'https://stackoverflow.com'
  },
  {
    id: 2,
    title: 'test4',
    url: 'https://stackoverflow.com'
  },
  {
    id: 5,
    title: 'test2',
    url: 'https://stackoverflow.com'
  }
];


const watched_videos = [
  {
    watched: true,
    video_id: 12
  },
  {
    watched: false,
    video_id: 32
  },
  {
    watched: true,
    video_id: 2
  }
];

const newListVideos = [];

video.forEach(item => {
  const searchResult = watched_videos.filter(element => element.video_id === item.id);
  if (searchResult.length > 0) {
    newListVideos.push(...searchResult);
  }
});

console.log(newListVideos);

Upvotes: 0

Related Questions