Delete elements with identical values from array "a" and then delete the element at the same index in array "b" as the one removed from array "a"

Currently, I am facing an issue while plotting a temperature chart as I have two arrays:

a, which consists of registered temperature values throughout the day. For example:

a=[22.1, 23.4, 21.7,...];

and b, containing the corresponding timestamps for each temperature value. For instance:

b= ["2016-12-15 16:50","2016-12-15 16:55","2016-12-15 17:00",...]

These two arrays are interconnected! If I pick the temperature value from

a[2],

it correlates to the timestamp in

b[2]

Here's what I aim to achieve:

I want to "merge" similar values. For instance, if my array a contains values like 22.1 at index 3, 4, 5, 6, and 7, I wish to combine them into one single 22.1 at index 3 of array a. Subsequently, I aim to eliminate all items from array b that correspond to indexes 4, 5, 6, and 7. Furthermore, I want to maintain the order within both array a and array b. Please note that this is just an example, and my actual intention is not always to remove elements indexed from 4 to 7.

Answer №1

It seems like the question is a bit difficult to grasp. However, one approach could be to create an array of objects based on arrays A and B. Each object would contain a time property and a temperature value, structured as follows:

var a = [22.1, 23.4, 21.7 ,22.1];
var b= ["2016-12-15 16:50","2016-12-15 16:55","2016-12-15 17:00", "2016-12-15 17:05"];
var temperatures = a.map((x,i) => ({"values": x, "time": b[i]}));
console.log(temperatures);

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 is the best way to dynamically adjust the size of a grid of divs to perfectly fit within the boundaries of a container div without surpassing them?

Currently, I am working on a project for "The Odin Project" that involves creating a web page similar to an etch-a-sketch. I have made some progress, but I am facing a challenge with dynamically resizing a grid of divs. The issue lies with the container d ...

What is the best way to modify the state of a nested component?

I am facing a challenge in my React project where I have two buttons in my App.js. When either of the buttons is clicked, I want to change the current state (list) to display in descending order based on the button pressed (order by date or order by upvo ...

Using Javascript to test a specific item in an asp Listbox

Let's consider a scenario where there is ListBox1 containing the following listitem: <asp:ListItem Value="No.1">No.1</asp:listitem> In addition, we have a label for a test purpose: <asp:Label ID="lblLabel" runat="server" Text="Label ...

Tips for incorporating a variable into a JSON object value when dynamically constructing an object

Is there a way to dynamically create an array of objects using a for loop? I have different arrays with values for the key value pairs of these objects. The code snippet I tried is not producing the expected result. var characters = ['Iron Man', ...

Guide to invoking an API in Next.js 13 by utilizing specific variables within a client component

I currently have a collection of products that are accessible on my website through a straightforward function within a server component. async function getData() { const res = await fetch(`${apiPath}`); const data = (await res.json()) as PackProps ...

Using JQuery, it is possible to search for elements by one specific class while excluding others

Looking for a way to target elements with a specific class, but only if they don't have another class attached to them. Here is an example: <li class="target-class exclude-class"></li> <li class="target-class exclude-class"></li& ...

retrieve the value of a specific key from an array

How can I extract key-value pairs from an array in JavaScript where the data is structured like this? jsonData = [ {"dimensions":[5.9,3.9,4.4,3.1,4.8],"icon":0,"curves": [false,false,false,false,false],"id":"p1","color":"0x000000"}, {"dimensio ...

The inner foreach loop will consistently capture the final iteration of the outer foreach loop

Here is a foreach loop with an inner foreach loop that needs some attention: foreach ($options_default as $key=>$value) { foreach ($option_names as $option_name_key=>$option_name_value){ $temp = array($key=>$value); ...

Using JavaScript, extract current date from an API data

Here is an example of how the data from my API appears: const data = [{ "id": "1", "name": "Lesley", "creationDate": "2019-11-21 20:33:49.04", }, { "id": "2", "name": "Claude", "creationDate": "2019-11-21 20:33:09.397", }, { "i ...

Prevent a form from loading depending on the response received from an ajax callback

I am currently working on implementing an ajax post function. The process involves sending data and receiving a callback from PHP with some data in return. Depending on the returned data, I need to make a decision whether to proceed or allow the user to re ...

Adjust the package.json file for deployment

I've encountered a problem while attempting to deploy my nodejs application on Heroku. Despite following the documentation and modifying files in the root directory, I have not been successful. Below is the structure of my package.json file: { ...

Using MeanJS to assign a Mongoose object reference to an array in Angular

Having an issue with MeanJS and using the $update function of the $resource service in Angular provided by MeanJS. Here is a basic outline of my problem: Mongoose schema: var mongoose = require('mongoose'), Schema = mongoose.Schema; var Lotion ...

Exploring the implementation of --history-api-fallback in webpack

let path = require('path') module.exports = { entry:path.resolve('public/src/index.js'), output: { path:__dirname + "/public", filename: "bundle.js" }, module: { loaders: [{ exclude: / ...

inter-site requests & browser extensions

I'm currently working on developing a Firefox Addon using the new WebExtensions system. My goal is to: Extract specific text from a webpage (not owned by me) Evaluate it using an external website Display the result on the same page The issue I&apo ...

How to enable CORS in Flask while avoiding the "Response to preflight request does not have an HTTP ok status" issue

Seeking assistance with setting up client-side Javascript code to send post requests to my Flask backend. I referenced this helpful answer regarding an issue with flask-cors being blocked by CORS policy, resulting in a preflight request error without passi ...

Changing an array of strings into a single string in Java

Could someone please provide me with the Java code to convert an array of strings into a single string? ...

How to utilize dot notation in HTML to iterate through nested JSON in AngularJS?

I'm struggling with displaying nested objects loaded from a JSON file in Angular. I've seen examples of using dot notations in HTML to access nested data, but I'm new to Angular and can't seem to get it right. The JSON is valid, but I j ...

Guide to implementing dynamic conditional rendering in Vue.js loops (utilizing v-if within v-for)

I am currently working on a table component in a .vue file where I want to display icons based on the direction of the order clicked. For example: <th v-for="(column, index) in columns" :key="index" @click="sort( index )"> <span& ...

Variations in how arrays are declared in Swift

When it comes to declaring in Swift, what sets apart var list = NSMutableArray() from var list:NSMutableArray = []? ...

Display a loading GIF for every HTTP request made in Angular 4

I am a beginner with Angular and I am looking for a way to display a spinner every time an HTTP request is made. My application consists of multiple components: <component-one></component-one> <component-two></component-two> <c ...