Categories
JavaScript Answers

How to use Array.includes() to find object in array with JavaScript?

To use Array.includes() to find object in array with JavaScript, we use the some method.

For instance, we write

const arr = [{ a: "b" }];
console.log(arr.some((item) => item.a === "b"));

to call arr.some with a callback that checks if item in arr has property a equal to 'b'.

If any object in arr has property a equal to 'b' then it returns true.

Categories
JavaScript Answers

How to split an array into two based on an index in JavaScript?

To split an array into two based on an index in JavaScript, we use the slice method.

For instance, we write

const ar = [1, 2, 3, 4, 5, 6];
const p1 = ar.slice(0, 4);
const p2 = ar.slice(4);

to call ar.slice with 0 and 4 to return an array with the items in ar between index 0 and 3 inclusive.

And we call ar.slice with 4 to return an array with the items in ar from index 4 to the end of the ar array.

Categories
JavaScript Answers

How to filter null from an array in JavaScript?

To filter null from an array in JavaScript, we use the filter method.

For instance, we write

const filtered = [1, "", null, NaN, 2, undefined, 4, 5, 6].filter((x) => x);

to call [1, "", null, NaN, 2, undefined, 4, 5, 6].filter with a callback that returns the parameter to return an array that only includes the truthy values in [1, "", null, NaN, 2, undefined, 4, 5, 6].

Categories
JavaScript Answers

How to filter array of objects with another array of objects with JavaScript?

To filter array of objects with another array of objects with JavaScript, we use the filter method.

For instance, we write

const myArray = [
  { userid: "100", projectid: "10", rowid: "0" },
  { userid: "101", projectid: "11", rowid: "1" },
  { userid: "102", projectid: "12", rowid: "2" },
  { userid: "103", projectid: "13", rowid: "3" },
  { userid: "101", projectid: "10", rowid: "4" },
];
const myFilter = [
  { userid: "101", projectid: "11" },
  { userid: "102", projectid: "12" },
  { userid: "103", projectid: "11" },
];

const myArrayFiltered = myArray.filter((el) => {
  return myFilter.some((f) => {
    return f.userid === el.userid && f.projectid === el.projectid;
  });
});

to call myArray.filter with a callback that checks if the properties of object el in myArray has the userid and projectid equal to object f in myFilter with some.

Then the items in myArray that matches any object property we check for in myFilter is returned.

Categories
JavaScript Answers

How to convert JSON object to CSV format in JavaScript?

To convert JSON object to CSV format in JavaScript, we use some object and array methods.

For instance, we write

const convertToCSV = (arr) => {
  const array = [Object.keys(arr[0])].concat(arr);

  return array
    .map((it) => {
      return Object.values(it).toString();
    })
    .join("\n");
};

console.log(
  convertToCSV([
    {
      id: 1,
      name: "Foo",
      timestamp: new Date(),
    },
    {
      id: 2,
      name: "Bar",
      timestamp: new Date(),
    },
    {
      id: 3,
      name: "Baz",
      timestamp: new Date(),
    },
  ])
);

to define the convertToCSV function.

In it, we create an array from the property names of the first object in arr we get from Object.keys.

We then add the entries off arr to the end of it with concat.

Next, we call map with a callback to get the values of each object with Object.values and call toString to convert the array to a comma separated string.

Then we call join to join each row with a new line.