Categories
JavaScript Answers

How to search for an object in an array of objects with JavaScript?

To search for an object in an array of objects with JavaScript, we use the array some method.

For instance, we write

const listOfObjecs = [
  { id: 1, name: "Name 1", score: 11 },
  { id: 2, name: "Name 2", score: 22 },
  { id: 3, name: "Name 3", score: 33 },
  { id: 4, name: "Name 4", score: 44 },
  { id: 5, name: "Name 5", score: 55 },
];

const object = { id: 3, name: "Name 3", score: 33 };
const booleanValue = listOfObjecs.some(
  (item) =>
    item.id === object.id &&
    item.name === object.name &&
    item.score === object.score
);

to call listOfObjects.some with a callback that checks if the id property of each item in listOfObjects equals object.id.

And we do the same with the name and score properties and combine the expressions with logical and.

Since the object that meets this condition exists in listOfObjects, true is returned.

Categories
JavaScript Answers

How to store an array of objects in local storage with JavaScript?

To store an array of objects in local storage with JavaScript, we convert it to a JSON string.

For instance, we write

localStorage.setItem("users", JSON.stringify(users));

to call JSON.stringify with the users array to convert it into a JSON string.

Then we call setItem to store the string as the value with key 'users'.

Categories
JavaScript Answers

How to get duplicate values from an array with JavaScript Lodash?

To get duplicate values from an array with JavaScript Lodash, we can use the filter and includes methods.

For instance, we write

const dups = _.filter(arr, (val, i, iteratee) =>
  _.includes(iteratee, val, i + 1)
);

to call filter with arr and a callback that calls includes to check if val is included in the arr array itself from index i + 1 and oin.

iteratee is the arr array.

An array with the duplicates is returned.

Categories
JavaScript Answers

How to convert a multidimensional JavaScript array to JSON?

To convert a multidimensional JavaScript array to JSON, we use the JSON.stringify method.

For instance, we write

const arr = [
  [1, 2, 3],
  [4, 5, 6],
];
const json = JSON.stringify(arr);

to call JSON.stringify with arr multidimensional array to convert arr into a JSON string.

Categories
JavaScript Answers

How to filter JSON by key value with JavaScript?

To filter JSON by key value with JavaScript, we use the filter method.

For instance, we write

const arr = [
  { time: "2016-07-26 09:02:27", type: "aa" },
  { time: "2016-04-21 20:35:07", type: "ae" },
  { time: "2016-08-20 03:31:57", type: "ar" },
  { time: "2017-01-19 22:58:06", type: "ae" },
  { time: "2016-08-28 10:19:27", type: "ae" },
  { time: "2016-12-06 10:36:22", type: "ar" },
  { time: "2016-07-09 12:14:03", type: "ar" },
  { time: "2016-10-25 05:05:37", type: "ae" },
  { time: "2016-06-05 07:57:18", type: "ae" },
  { time: "2016-10-08 22:03:03", type: "aa" },
  { time: "2016-08-13 21:27:37", type: "ae" },
  { time: "2016-04-09 07:36:16", type: "ar" },
  { time: "2016-12-30 17:20:08", type: "aa" },
  { time: "2016-03-11 17:31:46", type: "aa" },
  { time: "2016-05-04 14:08:25", type: "ar" },
  { time: "2016-11-29 05:21:02", type: "ar" },
  { time: "2016-03-08 05:46:01", type: "ar" },
];
const filtered = arr.filter((a) => a.type == "ar");

to call arr.filter with a callback that checks if thge type property in each object in arr is equal to 'ar'.

An array with the objects in arr with the type property equal to 'ar' is returned.