Merge two arrays together and pad with zeros where they do not overlap

Looking for help with combining two arrays containing datetimes and displaying the values on the x-axis of a chart.

In need of a function that merges the arrays into one, adding a '0' where there are no duplicates.

array 1 = [2016-01-20,2016-01-21,2016-01-24]
array 2 = [2016-01-21]

final array = [0, 2016-01-21, 0]

Any suggestions on how to do this efficiently?

Thank you in advance!

Answer №1

To achieve this, utilize the map() function along with indexOf().

var list1 = ['2016-01-20', '2016-01-21', '2016-01-24']
var list2 = ['2016-01-21']

var result = list1.map(function(item) {
  return (list2.indexOf(item) == -1) ? item = 0 : item;
});

console.log(result)

Answer №2

To determine if a value in the first array exists in the second array, simply iterate through the first array.

for ($i = 0; $i < count($array1); $i++) {
    if (!in_array($array1[$i], $array2)) {
        $array1[$i] = 0;
    }
}

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

Tips for extracting the menu key from a JSON object using AngularJS

Is there a way to access the "menu1" and "menu2" fields in AngularJS from the following JSON data? { "menu1": [ { "item": "1", "Auth": "content/articleList", }, { "item": "2", "Auth": "content/articleList", }], "menu2": ...

Animating and resizing a div following the addition of HTML content

As someone who is fairly new to jQuery, AJAX, and web development as a whole, this particular issue has been keeping me on edge. The AJAX request that I have implemented successfully pulls in content from another page into the current one. Now, my challen ...

How come only the final element is being displayed from an array in JavaScript, rather than all of the elements present

I am facing an issue while attempting to extract specific information from a JSON data and create a new array with key-value pairs. However, instead of getting all the elements, it only returns the last one. Here is my current code snippet: const input = ...

The PHP function is not successfully receiving a value from the AJAX call to be entered into the Database

I've been struggling with a piece of code lately, trying to pass the value 1 to my database when a user clicks the "collect coins" button. The goal is to reset the column "dailyfree" every day at 12 pm so that users can click the button again on the n ...

The message sent back by Django Rest Framework is: "a legitimate integer must be provided"

I have integrated a react form within my Django application, supported by the rest framework in the backend. When I submit the form without entering any value in the integer field, I encounter the following error message from the rest API: "a valid integer ...

Utilizing _.where() in underscore to perform case-insensitive value comparisons

I need help with a webpage feature that allows users to search for specific values in a file. However, I want the search functionality to be case-insensitive while keeping the original case sensitivity of the data in the file. Currently, I am using an und ...

Having trouble with the find method when trying to use it with the transform

In my code, I have three div elements with different values of the transform property assigned to them. I store these elements in a variable using the getElementsByClassName method and then try to find the element where the value of the transform property ...

An Overview of Implementing Ajax Calls for Partial Views on Form Submission

Check out my jQuery code below: <script type="text/javascript"> $("#submitfileform").submit(function () { $.ajax({ type: 'POST', contentType: 'application/html;charset=utf-8', dataT ...

Utilizing the malloc function to dynamically allocate memory for an array

My current challenge involves creating an array of structs using malloc to properly allocate the necessary memory. Here is my code snippet: typedef struct stud{ char stud_id[MAX_STR_LEN]; char stud_name[MAX_STR_LEN]; Grade* grd_list; Income* i ...

Setting up KCFinder integration in TinyMCE

I am utilizing TinyMCE as a text field, and I am in need of enabling image upload functionality within it. To achieve this, I am using KCFinder. However, I am encountering an issue where upon clicking on the 'Upload Images' button, only a white b ...

What could be causing the value of my variable tabledata to be undefined?

In my code, I am trying to achieve the functionality of returning a row from a table when it is clicked. This is accomplished using the jQuery function $("tr.table").click(function)..... Subsequently, I aim to store the data of this table row in ...

Enhance the functionality of selectize.js by incorporating select options through ajax

I'm currently working on implementing options to a select box using AJAX and selectize.js. When not using selectize.js, everything functions correctly. The two select boxes are interconnected so that when one is updated, the values in the other select ...

Ways to customize PreBid.js ad server targeting bidder settings

In an effort to implement a unique bidder setting key name within my prebid solution, I have taken the necessary steps as outlined in the documentation by including all 6 required keys. Is it possible to change the key name 'hb_pb' to 'zm_hb ...

When utilizing the useLocation feature of React-Router-DOM to retrieve data passed in React, it can cause manually inputted links to malfunction

When manually inputting a link that is incorrect (e.g., "/characters/test"), it initially works fine, but if the link is correct, it still redirects to error 404. However, clicking the link from the Character component functions properly. This me ...

Unable to cancel the RTK query request

It's quite a dilemma. I need to handle the request differently when there is no user present. I've attempted different approaches like this and that const { data: carts = [] as ICart[], isFetching } = api.useGetCartProductsQuery(user.id, { skip: ...

Encountered difficulties sending JSON data to a REST endpoint using Node.js

Is there a way to bulk insert JSON data into MongoDB using Mongoose? I am aware of the insertMany method, but I'm encountering difficulties with extracting the correct req.body. Below is an image of my setup in Postman. https://i.sstatic.net/xFznd.pn ...

A guide to effectively converting and parsing a class object that includes Uint8Array elements

After stringifying a class object that contains a property in Uint8Array, and then parsing it later, the property is no longer in Uint8Array format. Here's an example: class Example { title:string; byteData:Uint8Array; ...

Heroku experiencing instability with Javascript/MySQL project during requests

Currently facing a problem with my Heroku API developed in JavaScript that interacts with a MySQL database. Previously operational, now encountering an error on each API request: 2020-06-17T18:37:13.493711+00:00 app[web.1]: > <a href="/cdn-cgi/l/ema ...

Material-UI icons refusing to show up on the display

I've been working on creating a sidebar component and importing various icons to enhance the UI, but for some reason they are not displaying on the screen. I even tried other suggested solutions without success. <SidebarOption Icon = {InsertComment ...

Is it feasible to utilize a variable as a function within JavaScript programming?

I've been diving into the world of express.js and came across these statements. const express = require('express'); const app = express(); It's intriguing how we can call the variable "express" as a function by simply adding parenthese ...