Categories
JavaScript Answers

How to get a list of duplicate objects in an array of objects with JavaScript?

Sometimes, we want to get a list of duplicate objects in an array of objects with JavaScript.

In this article, we’ll look at how to get a list of duplicate objects in an array of objects with JavaScript.

How to get a list of duplicate objects in an array of objects with JavaScript?

To get a list of duplicate objects in an array of objects with JavaScript, we can use the array methods.

For instance, we write:

const values = [{
    id: 10,
    name: 'someName1'
  },
  {
    id: 10,
    name: 'someName2'
  },
  {
    id: 11,
    name: 'someName3'
  },
  {
    id: 12,
    name: 'someName4'
  }
];

const duplicateIds = values
  .map(v => v.id)
  .filter((v, i, vIds) => vIds.indexOf(v) !== i)
const duplicates = values
  .filter(obj => duplicateIds.includes(obj.id));
console.log(duplicates)

to get an array of value entries with the same id and put them into duplicates.

To do this, we get the ids of the items with the same id by calling map to get the ids into their own array.

Then we call filter with a callback that checks if indexOf returns the same value as index i to get all the ids of the duplicate items in the duplicateIds array.

Next, we call values.filter with a callback to check if the id of the item is in the duplicateIds array.

As a result, we get:

[
  {
    "id": 10,
    "name": "someName1"
  },
  {
    "id": 10,
    "name": "someName2"
  }
]

for the value of duplicates.

Conclusion

To get a list of duplicate objects in an array of objects with JavaScript, we can use the array methods.

Categories
JavaScript Answers

How to convert object’s properties and values to array of key value pairs with JavaScript?

Sometimes, we want to convert object’s properties and values to array of key value pairs with JavaScript.

In this article, we’ll look at how to convert object’s properties and values to array of key value pairs with JavaScript.

How to convert object’s properties and values to array of key value pairs with JavaScript?

To convert object’s properties and values to array of key value pairs with JavaScript, we can use the Object.entries method.

For instance, we write:

const obj = {
  value1: 'prop1',
  value2: 'prop2',
  value3: 'prop3'
};
const pairs = Object.entries(obj)
console.log(pairs)

We call Object.entries with obj to return an array of key-value arrays from the obj object.

As a result, pairs is:

[
  [
    "value1",
    "prop1"
  ],
  [
    "value2",
    "prop2"
  ],
  [
    "value3",
    "prop3"
  ]
]

The first entry of each nested array is the key and the 2nd entry is the value of the corresponding key.

Conclusion

To convert object’s properties and values to array of key value pairs with JavaScript, we can use the Object.entries method.

Categories
JavaScript Answers

How to change the page title with JavaScript?

Sometimes, we want to change the page title with JavaScript.

In this article, we’ll look at how to change the page title with JavaScript.

How to change the page title with JavaScript?

To change the page title with JavaScript, we can set the document.title property.

For instance, we write:

document.title = 'New Title';

to set the page title to ‘New Title’.

Conclusion

To change the page title with JavaScript, we can set the document.title property.

Categories
JavaScript Answers

How to get position of the element of the array with JavaScript?

Sometimes, we want to get position of the element of the array with JavaScript.

In this article, we’ll look at how to get position of the element of the array with JavaScript.

How to get position of the element of the array with JavaScript?

To get position of the element of the array with JavaScript, we can use the array’s indexOf method.

For instance, we write:

const pos = [1, 2, 3, 4].indexOf(3);
console.log(pos)

to call indexOf on [1, 2, 3, 4] with 3 to get the index of 3 in the array.

Therefore, pos is 2 since 3 is in the 3rd position in the array.

Conclusion

To get position of the element of the array with JavaScript, we can use the array’s indexOf method.

Categories
JavaScript Answers

How to add a boolean attribute to an element using JavaScript?

Sometimes, we want to add a boolean attribute to an element using JavaScript.

In this article, we’ll look at how to add a boolean attribute to an element using JavaScript.

How to add a boolean attribute to an element using JavaScript?

To add a boolean attribute to an element using JavaScript, we can call setAttribute to set the boolean attribute to the same value as the attribute name to add the attribute.

We can use removeAttribute to remove the attribute.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.setAttribute('disabled', 'disabled');

setTimeout(() => {
  input.removeAttribute('disabled');
}, 3000)

to select the input with querySelector.

Then we call input.setAttribute with 'disabled' and 'disabled' to set the disabled attribute to disabled.

And then we call input.removeAttribute with 'disabled' in the setTimeout callback to remove the attribute in 3 seconds.

Conclusion

To add a boolean attribute to an element using JavaScript, we can call setAttribute to set the boolean attribute to the same value as the attribute name to add the attribute.

We can use removeAttribute to remove the attribute.