Verify that all keys of an object are present as values in an array of objects

Imagine having two entities like the following:

const obj1 = {key1: "", key2: "", key3: ""};
const array2 = [
  {
    name: "key1",
  }]

Is there a way to verify if array2 contains an object with the same name as each field in obj1?

In essence, I want to transform array2 into this form:

const array2 = [
  {
    name: "key1",
  },
{
    name: "key2",
  },
{
    name: "key3",
  }]

Answer №1

Is this similar to what you had in mind?

const obj1 = {
    attr1: "",
    attr2: "",
    attr3: ""
};

const array2 = [{
    name: "attr1",
}, {
    name: "attr2",
}];

const objAttrs = Object.keys(obj1);
const array2Names = array2.reduce((array2Names, item) => {
    array2Names.push(item.name);
    return array2Names;
}, []);

for (let j = 0; j < objAttrs.length; j++) {
    if (!array2Names.includes(objAttrs[j])) {
        console.log(objAttrs[j], ' is not present in the array2');
        break;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to integrating numerous product filters for an ECommerce platform using Spring Boot, JavaScript, Ajax, and MySql

Currently, I am developing an E-Commerce Web App using Spring Boot which includes a feature to add multiple product filters. The user can select checkboxes corresponding to their preferences for filtering or searching products from the store. However, I am ...

Employing angular-ui-bootstrap to display pop-up notifications

I am new to using Angular and TypeScript and I am attempting to create a popup when an image is clicked. I came across a post on Stack Overflow that provides a solution using IMODALSERVICE. How to utilize angular-ui-bootstrap modals with TypeScript? Howe ...

in the matchMedia addListener callback function, you can pass an argument

While working on a plugin that involves toggling certain behaviors on and off using matchMedia functions, I encountered an issue with passing arguments to my callback function. Whenever I try to pass an argument to the callback function, it returns as unde ...

Tips for adding and verifying arrays within forms using Angular2

Within my JavaScript model, this.profile, there exists a property named emails. This property is an array composed of objects with the properties {email, isDefault, status}. Following this, I proceed to define it as shown below: this.profileForm = this ...

What are the appropriate situations to utilize Q.defer versus using Promise.resolve/reject?

I've been working with nodejs and I'm curious about when to use Q defer over Promise.resolve/reject? There are numerous examples of both methods, such as: // using Q defer function oneWay(myVal) { var deferred = Q.defer(); if (myVal < 0) ...

How can you trigger a link click event when clicking anywhere on the page using Jquery?

Here's the code I'm working with: <a href="http://google.com" target="_blank">Open in new tab </a> I am trying to make it so that when a user clicks anywhere on the website, the link above will be automatically clicked and a new tab ...

Nested Angular Controller in a ng-repeat loop

I am attempting to utilize a nested controller within an ng-repeat in order to have the accordion panels on the page function in different scopes. However, I am encountering a problem where the code inside the nested controller is not being executed. I pla ...

Saving user input as a local variable to be utilized on a different web page

I am attempting to capture a user input from a form and then utilize that variable on a separate page. This process should be straightforward, but I am not receiving any results and I am unsure of the reason why. Here is the code on the first page: <di ...

The textfields within the .map are not aligned next to each other

Adding the .map caused the name and quantity text fields to no longer display side by side. Before adding the .map, these two fields were in the same row but now they are not. <Grid container rowSpacing={1} columnSpacing={{ xs: 1, sm: 2, md: 3 }}> ...

What is the significance of the error message '[WDS] Disconnected!' in the context of using webpack and Vue.js?

Currently, I am engaged in a Django project that utilizes Vue.js for the frontend. Whenever I refresh the page, I encounter the "[WDS] Disconnected!" error. Despite the website's full functionality and absence of issues, this error appears every time ...

Exploring the world of AngularJS and Packery

After stumbling upon this particular question, I was thrilled to find the perfect answer that suits my needs. Now I am seeking guidance on how to implement the solution provided in this question. Is there a way to make the necessary changes for utilizing n ...

Namespacing is not applied to dynamic modules in Vuex

I've been tackling a modular vue application that enrolls the modules during compile time. Take a look at the code snippet below - app.js import store from './vue-components/store'; var components = { erp_inventory: true, erp_purc ...

"Critical issue: Meta tags are not present on the dynamic pages of the NextJS

In my NextJS application, the pages are structured as follows: App --pages ----_app.js ----index.js ----company.js ----users ------[userID].js I have a dynamic page named [userID].js that retrieves the userID through the router to display information for ...

Guide to implementing Vuetify tabs alongside Vue Router

I have a scenario with two Vuetify tabs in this jsfiddle, but the documentation lacks examples of using vue-router with them. After stumbling upon a helpful Medium.com post about integrating Vuetify with vue-router, I came across the following code: <d ...

Is there a way to incorporate tailwind classes into each react component when utilizing the map() function?

While working on a React Movie app, I encountered an issue with the tailwind class mx-auto. It was not applying properly to all items in the array; only the first element was affected. const MoviesList = () => { return( <div className=&a ...

How can you adjust the placement of a text geometry?

I have scoured through various resources like stack overflow and google in search of a solution to my problem. I have managed to successfully create a block of text in my scene that reads "Buy Here!". However, I am facing difficulty in offsetting the text ...

Mapping URLs to objects in JavaScript, TypeScript, and Angular4

I have implemented a class called SearchFilter class SearchFilter { constructor(bucket: string, pin: number, qty: number, category: string) { } } When the user performs a search, I populate the filter i ...

What is the best way to populate MongoDB with information retrieved from an external API?

Recently, I've been diving into the world of NodeJS with mongoose & mLab. As someone new to these technologies, I'm slowly piecing together my model. Here's what it looks like for now, with plans to expand the schema down the road. cons ...

Customize Meta tags in Next.js 13.4

I am facing an issue with overriding the meta tag in my RootLayout.js file. Whenever I try to add a new viewport meta tag, it ends up duplicating the existing one. Can anyone help me find a solution to this problem? RootLayout.js <html lang="en&q ...

Can Angular i18n facilitate language switching?

My objective is to switch the language from English (United States) to Indonesia using a button. View Source Code https://i.sstatic.net/0YlfWaCY.gif The issue is that the tutorial does not cover how to implement the language change feature. Why opt for ...