-3

So basically I have an array of ids and I want to return only a simple array which I want to look like this

*[1,2,3]*

instead of

*[0:[1] , 1:[2]]*

Is there any way to do it

Code

const usersWhoHavePurchasedYourCourses = usersWhoHaveTokens.filter(
(user1: any) => {
  return user1.tokens
    ?.map((token: any) => parseInt(token.course_id))
    .includes(user.courses?.map((course: any) => course.id));
});

The output looks like this

output

As I said I don`t want to return this kind of output.

Updater XD
  • 261
  • 1
  • 3
  • 8
  • Please read the "*[ask]*" and "*[mcve]*" guidelines, we need enough of your code to reproduce your problem and enough detail around the context that we can understand what you're trying to do, and why. Also, where does the `3` come from, and the last line of your code (following "*instead of*") seems invalid. – David Thomas Jan 27 '23 at 17:01
  • Returning is something you do from a function. Where is your function? `[0:[1] , 1:[2]]` is meaningless in JavaScript, and `[1,2,3]` is an Array with three numbers. Where in the process of writing your code are you stuck? What exactly is the issue? Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212), how to [access properties](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_Accessors), and how to create [arrays](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/Array#array_literal_notation). – Sebastian Simon Jan 27 '23 at 17:02
  • Your output is `[ [ 1 ], [ 2 ] ]`. Please _right click_ → “Copy object”. Duplicate of [Merge/flatten an array of arrays](/q/10865025/4642212). – Sebastian Simon Jan 27 '23 at 17:13
  • Does this answer your question? [Merge/flatten an array of arrays](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) – Daniel Widdis Jan 27 '23 at 18:30

1 Answers1

0

Edit

In attempting to reverse-engineer your logic, wouldn't you want to filter by checking if a user has at least one course? I recommend using Array.prototype.some as your filter result.

const user = { courses: [{ id: 1 }, { id: 2 }] };

const usersWhoHaveTokens = [
  { id: 1, tokens: [{ course_id: '1' }] },
  { id: 2, tokens: [{ course_id: '2' }] },
  { id: 3, tokens: [{ course_id: '3' }] },
];

// Compute the set, for faster processing
const userCourseIds = new Set(user.courses.map((course) => course.id));

const usersWhoHavePurchasedYourCourses = usersWhoHaveTokens
  .filter(({ tokens }) => tokens
    .some((token) => userCourseIds.has(parseInt(token.course_id))))
  .map(({ id }) => id);

console.log(usersWhoHavePurchasedYourCourses); // [1, 2]

Original response

If you object is an 'object' type, you will need to transform it into an array, and then flatten it.

const
  obj = { 0: [1], 1: [2] },
  arr = Object.values(obj).flat();

console.log(JSON.stringify(arr)); // [1, 2]

If you want to preserve indices:

const
  obj = { 1: [2], 5: [6] },
  arr = Object.entries(obj).reduce((acc, [index, value]) => {
    acc[+index] = value;
    return acc;
  }, []).map(([value]) => value);

console.log(JSON.stringify(arr)); // [null, 2, null, null, null, 6]
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132