What is the best way for an object to recognize if its value matches a key in another object, and then retrieve that value

Is there a way to make an object detect if its value is equal to another object's key and then retrieve it?

Let's consider the following object named:

AnswerString

let AnswerString ={
0: "B",
1: "x"
}

and the answers are in different objects, but for simplicity let's focus on just one object called M2060

Let's define M2060 as:

var M2060 = {
  "B" : [
    "v-40 a20,10 0 0 1 0,20 a20,10 0 0 1 0,20",
   ],
   "x" : [
    "l20,-20 l-10,10 l10,10 l-10,-10 l-10,-10"
   ],
}

The name M2060 is based on it being the starting move to function for the SVG 'd' attribute. With that, how can I make 0 and 1 equal to the values in AnswerString?


AnswerString = {
0: "v-40 a20,10 0 0 1 0,20 a20,10 0 0 1 0,20",
1: "l20,-20 l-10,10 l10,10 l-10,-10 l-10,-10"
}

Should I create another object with these values and if so, how can I do that? I have been attempting:

if (M2060[answersString]) {
  let solution1 = M2060[answersString]); 
}

However, it seems that this approach is not working. EDIT: It seems I was missing .Entries and map methods.

Answer №1

It appears that the original poster is attempting to swap values in one object with values from another object, using the initial values as keys.

let AnswerString = {
  0: "B",
  1: "x"
};

let M2060 = {
  "B": [
    " v-40  a20,10 0 0 1 0,20   a20,10 0 0 1 0,20 "
  ],
  "x": [
    "  l20,-20  l-10,10 l10,10 l-10,-10 l-10,-10 "
  ]
};

let result = Object.fromEntries(
  Object.entries(AnswerString).map(([k, v]) => [k, M2060[v]])
);

console.log(result);

Object.entries() provides an array of key-value pairs. This approach converts entries by retrieving values from the other object and returning the original object keys. Object.fromEntries() then generates a new object using the modified array of key-value pairs.

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

Spin the object at regular intervals

Recently, I stumbled upon this interactive Pen: https://codepen.io/golle404/pen/BoqrEN that caught my eye. I thought it would be interesting to make the object move every few seconds. My first attempt involved using the following code snippet: setTimeout( ...

Navigating the division between presentational and container components in React/Redux

Here is a basic outline of my current setup: container.js: import Presentation from './Presentation'; import { connect } from 'react-redux'; export default connect( ({ someState}) => ({ ...someState }), (dispatch) => { ...

Issue with getStaticProps functionality on Vercel seems to be persisting

UPDATE: I realize now that my mistake was calling my own API instead of querying the MongoDB database directly in getStaticProps(). I have made the necessary changes: export async function getStaticProps() { await dbConnect(); const user = await User.find ...

What is the way to include an additional filter in an AngularJS search filter?

I currently have a functioning search filter in place that utilizes a search input element: <input placeholder="Search" type="text" ng-model="search.$"/> Moreover, I have: <tr ng-repeat="person in people | filter:search:strict"> <td> ...

Transcluding an element into an ng-repeat template in AngularJS: How can it be done?

Incorporating a carousel directive involves chunking the passed in array of items and mapping it into an array of arrays of elements. This structure then generates markup resembling the pseudo code provided below: <array of arrays> <array of i ...

Determine the previous day's date using JavaScript by subtracting one day

I am facing an issue while attempting to store a specific date format in MongoDB. Instead of saving the date as intended, it is consistently registering a day earlier than specified. For instance - db.test.insert({name: "test1", dob: new Date(1986, 11, ...

Turn off a feature

I'm having trouble disabling a tooltip that is being utilized from this specific website. The code for setting and calling the tooltip looks like this: this.tooltip = function(){....} $(document).ready(function(){ tooltip(); }); I've attempte ...

Progress Bar Aligned in the Middle

I am having difficulty centering the percentage total within the colored progress bar. I have attempted placing the <p> tag in various <div> tags but haven't been able to figure it out. Can someone provide assistance? ...

What is the reason that modifying a textarea causes the AJAX loading of content to be interrupted?

I am currently developing a feature that allows users to quote comments on my website. When a user wants to reply to a specific comment, they can simply click on the "quote" button next to that comment. This action triggers a script that adds the quoted co ...

Using React JS to send an object as an argument to an action function

How do I pass the "fields" object to the action "createPost" after creating it with validation in a form? Do I need to use "props" or could it be related to the dispatcher line? import React, { Component } from 'react'; import Header from ' ...

The option chosen from the ng-options does not remain attached to the drop-down menu

I need some help with AngularJS as I am still getting the hang of it. Take a look at this not-so-pretty page: https://i.sstatic.net/Dtm7j.png This page allows users to add new beers, and each beer can have multiple styles. Clicking 'Add' create ...

Ways to evaluate negative numbers in JavaScript

I need to validate latitude and longitude values within the range of -180 to 180. If a number falls outside of this range, an error should be displayed. Below is the code I have written for this validation: if((markerPoint[0] && parseInt(markerPoint[0] ...

Tips for eliminating the backslash introduced by JQuery

Switching back from framework 4.5 to 4.0 caused several issues that needed fixing. One significant change I noticed was that jQuery started escaping double quotes. Is there a way to stop this behavior? I attempted datatest = datatest.replace("\\ ...

Webpack encounters difficulty resolving non-js `require`s located within node_modules

In my Next.js project, I have set up the configuration to handle imports ending in .web.js, which works fine except for files within the node_modules directory. For this setup, I adjusted the webpack config by setting resolve.extensions = ['.web.js&ap ...

What is the best way to manage a promise's response?

I'm currently facing a situation where I need to create a petition to an endpoint and retrieve the URL of the backend that I intend to use. My setup involves a file with the API configuration where I instantiate axios. Previously, my approach was sim ...

absence of an export called

I am facing an issue with importing a simple component in my React project. I am unable to locate the component causing this error. The error message I am receiving while importing the component is as follows: ./src/App.js 61:28-32 './componentes/ ...

Attempting to determine income in JavaScript with the inclusion of two distinct rates

Trying to come up with a salary calculation for different rates within the same timeframe is proving to be quite challenging. For instance, between 10:00-15:00 the rate is $20 per hour and from 15:00 onwards it drops to $15 per hour. Check out the entire ...

"Using Angular's NgFor directive to efficiently organize and collapse data within

I am currently working on displaying API data in a table using ngFor, and I am facing an issue with hiding/showing a specific row of details outside the ngFor loop. Since this line is not within the ngFor loop, I am unable to bind the data accordingly. Can ...

What causes my input field to lose focus in React.js after typing just one character?

My react.js component is experiencing an issue where the input field loses focus whenever a character is typed. To continue typing or editing, I have to click on the input field again. What could be causing this problem? Below is the code snippet in quest ...

What is the best way to implement backup style attributes for a React JS component?

Is there a method to implement fallback style properties for a component? For example: var inlineStyle = { display: '-webkit-box', display: '-webkit-flex', display: '-moz-box', display: '-moz-flex', ...