logo
Tags down

shadow

Merge objects with with missing key


By : Benjamín Dionisio Do
Date : October 17 2020, 08:10 AM
wish help you to fix your issue Lodash's _.mergeWith() callback gets the keys as the 3rd param. You can use it to decide how to merge the items:
code :
const object1 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]}
const object2 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]}

const mergefn = (...args) =>
  _.mergeWith({}, ...args, (o, s, k) => {
      if(_.eq(k, 'timestamp')) return new Date(s);
      if(_.eq(k, 'count')) return (o || 0) + s;
  });
  
const result = mergefn(object1, object2);

console.log(result);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>


Share : facebook icon twitter icon

If property values of objects are equal in two array of objects then merge those objects into single object


By : sportsben
Date : March 29 2020, 07:55 AM
I wish this helpful for you You could use the stringified version of theobject to check for duplicates:
code :
const unique = (getKey, s = new Set) => el => !s.has(getKey(el)) && s.add(getKey(el));

const result = data3.concat(data4).filter( unique(el => JSON.stringify(el)) );

R: Why is merge dropping data? How to interpolate missing values for a merge


By : user2122899
Date : March 29 2020, 07:55 AM
it should still fix some issue In the end, I answered this question with a better understanding of the data. The merge function itself was not dropping any values, since it was only doing exactly as one tells it. However, since datasets were merged by SiteID, Year, Mo, Day, Hr the result was Inf, NaN, and NA values for a few SiteID.
The reason for this is that dB is not a fully continuous dataset to merge with. Thus, Inf, NaN, and NA values for some SiteID were returned because data did not overlap in all variables (SiteID, Year, Mo, Day, Hr).
code :
library(imputeTS)
library(tidyverse)

### We want to first interpolate dB values on the siteID first in dB dataset, BEFORE merging. 
### Why? Because the merge drops all the data that would help with the interpolation!!

dB<-read.csv("dB.csv")

dB_clean <- dB %>%
  mutate_if(is.integer, as.character)

# Create a wide table with spots for each minute. Missing will
#   show up as NA's
# All the NA's here in the columns represent 
#   missing jDays that we should add. jDay is an integer date 'julian date'
dB_NA_find <- dB_clean %>%
  count(SiteID, jDay) %>%
  spread(jDay, n)

dB_NA_find
# A tibble: 59 x 88
# SiteID `13633` `13634` `13635` `13636` `13637` `13638` `13639` `13640` `13641` 
# <fct>    <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>  
#   1 CU1D        NA      NA      NA      NA      NA      NA      NA      NA     
# 2 CU1M        NA      11      24      24      24      24      24      24      
# 3 CU1U        NA      11      24      24      24      24      24      24      
# 4 CU2D        NA      NA      NA      NA      NA      NA      NA      NA      
# 5 CU2M        NA       9      24      24      24      24      24      24      
# 6 CU2U        NA       9      24      24      24      24      21      NA      
# 7 CU3D        NA      NA      NA      NA      NA      NA      NA      NA      
# 8 CU3M        NA      NA      NA      NA      NA      NA      NA      NA      
# 9 CU3U        NA      NA      NA      NA      NA      NA      NA      NA     
# 10 CU4D        NA      NA      NA      NA      NA      NA      NA      NA     


# Take the NA minute entries and make the desired line for each
dB_rows_to_add <- dB_NA_find %>%
  gather(jDay, count, 2:88) %>%
  filter(is.na(count)) %>%
  select(-count, -NA) 

# Add these lines to the original,  remove the NA jDay rows 
#   (these have been replaced with jDay rows), and sort
dB <- dB_clean %>%
  bind_rows(dB_rows_to_add) %>%
  filter(jDay != "NA") %>%
  arrange(SiteID, jDay)


length((dB$DailyL50.x[is.na(dB$DailyL50.x)])) ## How many NAs do I have?
# [1] 3030

## Here is where we do the na.interpolation with package imputeTS
# prime the for loop with zeros
D<-rep("0",17)
sites<-unique(dB$SiteID)

for(i in 1:length(sites)){
  temp<-dB[dB$SiteID==sites[i], ]
  temp<-temp[order(temp$jDay),]
  temp$DayL50<-na.interpolation(temp$DailyL50.x, option="spline")
  D<-rbind(D, temp)
}

# delete the first row of zeros from above 'priming'
dBN<-D[-1,]

length((dBN$DayL50[is.na(dBN$DayL50)])) ## How many NAs do I have?
# [1] 0
dBN$Year<-"2017"  #all data are from 2017

##I could not figure out how jDay was formatted, so I created a manual 'key' 
##to get Mo and Day by counting from a known date/jDay pair in original data

#Example:
# 13635 is Mo=5 Day=1
# 13665 is Mo=5 Day=31
# 13666 is Mo=6 Day=1
# 13695 is Mo=6 Day=30

key4<-data.frame("jDay"=c(13633:13634), "Day"=c(29:30), "Mo"=4)
key5<-data.frame("jDay"=c(13635:13665), "Day"=c(1:31), "Mo"=5)
key6<-data.frame("jDay"=c(13666:13695), "Day"=c(1:30), "Mo"=6)
key7<-data.frame("jDay"=c(13696:13719), "Day"=c(1:24), "Mo"=7)

#make master 'key'
key<-rbind(key4,key5,key6,key7)

# Merge 'key' with dataset so all rows now have 'Mo' and 'Day' values
dBM<-merge(dBN, key, by="jDay", all.x=TRUE)

#clean unecessary columns and rename 'Mo' and 'Day' so it matches PC17 dataset
dBM<-dBM[ , -c(2,3,6:16)]
colnames(dBM)[5:6]<-c("Day","Mo")

#I noticed an issue with duplication - merge with PC17 created a massive dataframe
dBM %>%  ### Have too many observations per day, will duplicate merge out of control.
  count(SiteID, jDay, DayL50) %>% 
  summarise(
    min=min(n, na.rm=TRUE),
    mean=mean(n, na.rm=TRUE),
    max=max(n, na.rm=TRUE)
  )

## to fix this I only kept distinct observations so that each day has 1 observation
dB<-distinct(dBM, .keep_all = TRUE)
### Now run above line again to check how many observations per day are left. Should be 1
> PCdB<-(merge(PC17, dB, by=c("SiteID", "Year","Mo","Day"), all.x=TRUE, all=FALSE,no.dups=TRUE))
> ### all.x=TRUE is important. This keeps all PC17 data, even stuff that DOESNT have dB data that corresponds to it.

> library(dplyr)

#Here is the NA interpolated 'dB' dataset 
> dB %>% 
+   group_by(SiteID) %>% 
+   dplyr::summarise(
+     min_dBL50=min(DayL50, na.rm=TRUE),
+     max_dBL50=max(DayL50, na.rm=TRUE),
+     mean_dBL50=mean(DayL50, na.rm=TRUE),
+     med_dBL50=median(DayL50, na.rm=TRUE)
+   )
# A tibble: 59 x 5
   SiteID min_dBL50 max_dBL50 mean_dBL50 med_dBL50
   <chr>      <dbl>     <dbl>      <dbl>     <dbl>
 1 CU1D        44.7      53.1       49.4      50.2
 2 CU1M        37.6      65.2       59.5      62.6
 3 CU1U        35.5      51         43.7      44.8
 4 CU2D        42        52         47.8      49.3
 5 CU2M        38.2      49         43.1      42.9
 6 CU2U        34.1      53.7       46.5      47  
 7 CU3D        46.1      53.3       49.7      49.4
 8 CU3M        44.5      73.5       61.9      68.2
 9 CU3U        42        52.6       47.0      46.8
10 CU4D        42        45.3       44.0      44.6
# ... with 49 more rows

# Now here is the PCdB merged dataset, and we are no longer missing values!
> PCdB %>% 
+   group_by(SiteID) %>% 
+   dplyr::summarise(
+     min_dBL50=min(DayL50, na.rm=TRUE),
+     max_dBL50=max(DayL50, na.rm=TRUE),
+     mean_dBL50=mean(DayL50, na.rm=TRUE),
+     med_dBL50=median(DayL50, na.rm=TRUE)
+   )
# A tibble: 60 x 5
   SiteID min_dBL50 max_dBL50 mean_dBL50 med_dBL50
   <chr>      <dbl>     <dbl>      <dbl>     <dbl>
 1 CU1D        44.8      50         46.8      47  
 2 CU1M        59        63.9       62.3      62.9
 3 CU1U        37.9      46         43.6      44.4
 4 CU2D        42.1      51.6       45.6      44.3
 5 CU2M        38.4      48.3       44.2      45.5
 6 CU2U        39.8      50.7       45.7      46.4
 7 CU3D        46.5      49.5       47.7      47.7
 8 CU3M        67.7      71.2       69.5      69.4
 9 CU3U        43.3      52.6       48.1      48.2
10 CU4D        43.2      45.3       44.4      44.9
# ... with 50 more rows

Missing buttons for “Merge changes in merge tool”, “Take server version” etc. in VStudio 2019


By : user2761204
Date : March 29 2020, 07:55 AM
wish of those help Latest Visual Studio 2019, buttons for “Merge changes in merge tool”, “Take server version” etc. are missing. We use Team Foundation Server as source control. , This issue has been fixed in later Visual Studio 2019 updates.

Merge Objects: Problem with comparing and merge two objects in javascript


By : user3339930
Date : March 29 2020, 07:55 AM
I hope this helps you . Good night. Some charitable soul to help me with a problem about javascript objects. Thank you very much who can ... , This will work, it also adds up the counts for matching language
code :
var arrA = [
    {language:'php', count:5},
    {language:'c', count:7}
];

var arrB = [
    {language:'php', count:0},
    {language:'c', count:0},
    {language:'python', count:0},
    {language:'ace', count:0},
    {language:'electron', count:0},
];

let result = [...arrA, ...arrB].reduce((acc, item) => {
  let found = acc.find(x => x.language=== item.language);
  if (found) {
    found.count += item.count;
  } else {
    acc.push(Object.assign({},item));
  }
  return acc;
}, [])
console.log(result);

Angularjs merge two objects ignoring null and missing values


By : Harpreet
Date : March 29 2020, 07:55 AM
should help you out You can remove the null properties in object 2 before calling the extend.
Related Posts Related Posts :
  • Session.cookies.get_dict() return an empty dictionary
  • Performing logical operation inside Higher Order Component on prop set
  • how to force the browser to run the javascript every time it called
  • REGEX get all content after a series of special characters
  • deduce amount issue, not the same value foreach execute
  • Regex to get individual function bodies from group of functions
  • Stop animation from executing if coming from an internal link
  • How to retrieve total of "else if"
  • Multiple JS slideshows on one page
  • Importing async functions in Javascript
  • How to handle state on array of checkboxes?
  • React Native - Redux ~ Props updating when not getting called
  • Fast way to add elements into an Uint8ClampedArray typed array
  • How to retrieve values from a checkbox group generated dinamically with jquery when page loads
  • Progress bar between two dates (fetch "var" from script), HTML
  • Why do Every time I start my function I get NaN
  • Angular routing change in url
  • Javascript - Change symbol on embedded tradingview script.text
  • How to select specific option?
  • Checking booleans from multiple arrays simultaneously, without multiple if statements
  • How to get a subarray?
  • Javascript JSON woes
  • Use Jquery animate to have a button move a box to the next corner
  • Why is this JavaScript not interpreted as a code block when semi-colon is used?
  • Reload another page when I click on button
  • Should functions that call a callback function be bound?
  • For loop print in single line separated by spaces - JS
  • Add Javascript Variable into asp-route-data
  • Two different POST requests are making use of the same variable
  • Convert JSOn object into a flat JSON Array
  • Manually adding to an HTML range input gives unexpected results
  • Refreshing component after making api call Reactjs
  • Google App Script : Finding The Last Blank Row
  • I cant for the life of me figure out whats wrong with this script
  • Order divs by ID in Javascript
  • set new element class property value
  • How to pass a java variable to a different jsp page containing javascript?
  • Locked it method in chai
  • React JS : history.push is not a function error and it isn't navigating to a different page onclick of swal
  • How can i set timeout for localstorage for Angular 2+?
  • Must use destructuring props assignment
  • Jest Compared values have no visual difference. when testing arrays
  • Uncaught Error: #90211 ExtensionName Error - Node.JS, Heroku, JavaScript App using FusionCharts
  • Is it possible in firebase auth to update user and custom claims at the same time node?
  • Filter out capitalized letters
  • Yet Another Google Firebase Error "Function returned undefined, expected Promise or value"
  • How do I address method in a Vue.js component from inside callback function?
  • What is the name of this feature in JavaScript?
  • Vue js using scope-slot and trying to use a property from within but having no visibility
  • Dynamic javascript table won't display iterations from array
  • How to start/end smoke.js animation by clicking a nav button?
  • Map array of strings as object properties
  • Appending new inputs with JS wipes previous ones
  • Chai Request With URL instead of JS File
  • React Jest Async Tests
  • Unordered list bullet not showing at an expected location
  • How to create an array from an object?
  • How to get prefix name from html tag
  • javascript - Simulate a click event (tap with finger) on a button in iOS
  • How to get the value of checkboxes in a kendo grid
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org