Searching for an item in an object using a key and then updating that value in the array - A step-by-step

var replaceArr = ['A','J','Q',10,2];

var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

Within this code snippet are two arrays: replaceArr and originalArr

The task at hand is to compare the values in replaceArr with those in originalArr, extracting the corresponding keys from the latter to replace in the former. After completion, replaceArr should look like this: [0,10,11,9,1]

Your assistance in this matter is greatly appreciated.

Answer №1

One way to achieve this is by utilizing the map and find methods from the array prototype.

var replaceArr = ["A", "J","Q",10,2]

var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

replaceArr = replaceArr.map(value => originalArr.find(object => object[value] !== undefined)[value]);
console.log(replaceArr);

Answer №2

To combine all the various objects in originalArr and then search for values from replaceArr within it, follow these steps:

const replaceArr = ['A','J','Q',10,2];

const originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

let arrWithOneObject = Object.assign(...originalArr);

let result = replaceArr.map(value => arrWithOneObject[value]);

console.log(result);

Answer №3

One way to accomplish this is by utilizing the JavaScript array forEach method

initialArray.forEach((value) => {
  var replaceIndex = replacementArray.indexOf(Object.keys(value)[0]);
  if (replaceIndex >= 0) {
    replacementArray[replaceIndex] = value[Object.keys(value)[0]];
  }
});

Answer №4

const lettersToNumbers = ['A','J','Q','10','2']
const numObject = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

numObject.forEach((item)=>{
  let property = Object.keys(item)[0];
  let index = lettersToNumbers.indexOf(property);
  if(index !== -1){
    lettersToNumbers[index] = item[property]
  } 
})

console.log(lettersToNumbers)

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

What techniques can be used to modify HTML elements that are dynamically added to the page?

Initially, the code I am working on queries the database to determine if there are any available workstations in the office. If workstations are available, "option" elements are dynamically added to a "select" element using jQuery: $('#idofselectel ...

Using Two JavaScript Functions with Identical Names in One HTML File

I am currently facing an issue with a function where the data is retrieved from the getValue() function. The following code snippet is a fragment. grid.js function gridLoadComplete(){ var data = getValue(); } Take into account the following H ...

Determine the total value derived from adding two option values together

I am attempting to calculate the sum of two select options and display the result. So far, my attempts have only resulted in returning the value 1. What I am aiming for is to add the values from n_adult and n_children, and then show the total under Numbe ...

Execute the script repeatedly

(Since Stack snippet is temporarily down, I included a jsfiddle link): Visit this link for the code I've been struggling to get this script to work. Here's the simple Javascript code I have: $(document).ready(function() { showMessage(); s ...

My React/Redux App is experiencing difficulties as the state fails to load

Currently, I am working on a project based on the Redux Documentation page. Instead of uploading it directly to Stack Overflow, I decided to share my progress on GitHub: https://github.com/davidreke/reduxDocumentationProject I have completed the instructi ...

How to toggle two classes simultaneously using JQuery

Check out this code snippet: http://jsfiddle.net/celiostat/NCPv9/ Utilizing the 2 jQuery plugin allows for the following changes to be made: - The background color of the div can be set to gray. - The text color can be changed to red. The issue arises wh ...

Concealing items by placing them strategically between the camera and certain objects in Three.js

At the moment, my project involves utilizing three.js with several objects in the scene. One of the key features I am working on is the ability to select an object and have all other objects between the camera and the selected one hidden or removed. I am ...

Customize Bootstrap radio buttons to resemble standard buttons with added form validation styling

Is there a way to style radio buttons to look like normal buttons while maintaining their functionality and form validation? I have two radio buttons that need styling but should still behave like radio buttons. Additionally, I am utilizing Bootstrap for s ...

Unexpected behavior with Ember.js Ajax request

I recently started using the Ember.js framework. While attempting to make an AJAX call from the framework, I encountered unexpected results. Below is the code snippet on the page - Link to the first AJAX call, which is functioning as intended. {{#linkT ...

Spinning an object using JQuery

I am currently working on a project to test my skills. I have set up a menu and now I want to customize it by rotating the icon consisting of three vertical lines by 90 degrees every time a user clicks on it. This icon is only visible on smartphones when t ...

Why does the method Array.concat on an extended class remove empty values in Node.js version 8.9.3?

I am experimenting with adding new functionality to Arrays in node.js without altering the original Array class to avoid any unintended consequences in other modules. During testing, I encountered some failures which led me to discover that the behavior o ...

AngularJS directive created for validation on blur

I am striving to replicate some of the features found in Angular 1.3.X for the app I am developing, with a particular focus on ensuring compatibility with IE 8. Unfortunately, this constraint means that I cannot utilize version 1.3.X. I have encountered di ...

Issues with dependencies in npx create react app

After initiating a new react project with npx create-react-app client, I have encountered an issue where the react-scripts command is not found, preventing me from running the start script. Is there a way to resolve this problem? Furthermore, when attempt ...

Positioning with Bootstrap CSS libraries

The text next to the logo in the navbar isn't aligning properly (refer to this link for the page -> ). Furthermore, the logo isn't positioning correctly either, and this issue is present in all the navbar codes. I'm utilizing CSS bootstr ...

Transform various strings containing special characters into an array using PHP

I am facing an issue with extracting clean data from multiple arrays containing strings retrieved from a WordPress database. The data is stored in a peculiar format within a table generated by a plugin, as shown below: print_r($results); Array ( ...

Building a like/dislike feature in Angular

Here is a snippet of code I have that includes like and dislike buttons with font-awesome icons: <ng-container *ngFor="let answer of question.answers"> <p class="answers">{{answer.text}} <i class="fa fa-hand-o-le ...

PubNub's integration of WebRTC technology allows for seamless video streaming capabilities

I've been exploring the WebRTC sdk by PubNub and so far, everything has been smooth sailing. However, I'm facing a challenge when it comes to displaying video from a client on my screen. Following their documentation and tutorials, I have writte ...

Rotating elements with timed jQuery

I'm having trouble getting the selector to rotate at different intervals. I attempted using an if/else statement to make the first rotation occur after 3 seconds and subsequent rotations occur after 30 seconds. Unfortunately, it's rotating every ...

The content was not displayed because the MIME type did not match

I've been facing a persistent error regarding MIME type mismatch for all of my CSS and JavaScript files. The setup I'm currently using involves a node.js server with Express and Express-Handlebars for routing and templates. It seems that the prob ...

Accessing a Specific Element in ng-repeat using Protractor

Currently, I am in the process of writing end-to-end tests for an application that is under development. I am specifically focusing on creating a test for alerts generated by the alert directive (Angular UI Bootstrap) within a wrapper directive. Below is t ...