Convert an empty value to 0 - javascript

Consider this / Please try this yourself in the chrome console.
data = [1,,2,,3]
now i want to replace the empty values with 0
data = [1,0,2,0,3]
I did:
data = data.map(e => {
if (e===undefined)
{
return 0;
}
else
{
return e;
}
});
But it is still returning the empty values as empty. what is right way to do this?

The problem is that map doesn't call the function for the missing elements of the array. From MDN:
callback is invoked only for indexes of the array which have assigned values, including undefined. It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value).
The same is true of forEach.
You need to loop through the array indexes rather than using one of the mapping functions.
for (let i = 0; i < data.length; i++) {
if (!(i in data)) {
data[i] = 0;
}
}
Note that using i in data makes it only skip nonexistent entries. If you have an explicit undefined it will be left, e.g.
[1, , undefined, 3, 4]
will become
[1, 0, undefined, 3, 4]
If you want the explicit undefined to be replaced, you can use i === undefined instead.

Array.from() is handy for this. It will use the length to iterate over the array so it won't skip undefined values like map() does. You can pass the value to its second parameter which is a function and return the value or 0:
let data = [1,,2,,3]
let new_array = Array.from(data, i=> i || 0)
console.log(new_array)

Trying looping over the length of the array and filling the gaps:
for(var i = 0; i < data.length; i++)
{
if(typeof(data[i]) === 'undefined')
{
data[i] = 0;
}
}

Related

Backwards loop only printing undefined

I am attempting to create a function that can take in an array and display its contents backwards. I am having trouble with understanding why my function call is showing undefined when I enter an array in its parameter.
var arrayOne = []
function printReverse(arrayOne) {
for(var i = arrayOne.length-1; i < 0; i--) {
console.log(arrayOne[i])
}
}
There is a misunderstading with your question:
What you want to achieve is console.log elements on screen, not return anything.
Your code
var arrayOne = []
function printReverse(arrayOne) {
for(var i = arrayOne.length-1; i < 0; i--) {
console.log(arrayOne[i])
}
}
Does not work because you have a wrong operator in your code at i < 0. This will return false at first iteration, because i will be arrayOne.length, which would be > 0 if there is any element on it.
Change this part to i >= 0 and your code will work and actually print the values on console.
However, if you really want to have a reverted array, then you should simply use Array reverse() instead of writing a function to return it.
so there are some fundamentals that are off there. as stated in another answer i will never be less than 0 because you are defining it as a value greater than 0 in your for loop. Give something like this a try
EDIT: the comments are correct in the sense that the array will be mutated so make a copy of the array first which I've added using the spread operator
Also as far as this returning undefined -- it should return undefined unless you comment out the return statement
const arrayOne = [];
function printReverse(array) {
if (!Array.isArray(array) && array.length === 0 ) {
return 'The array is empty';
}
const arrCopy = [...array];
// technically you could just reverse it
// if you return it you have to assign it to someone on the function call
// return arrCopy.reverse();
// if you want to log the reversed array you could also
// console.log(arrCopy.reverse());
// if you want to reverse it then log each single index
// arrCopy.reverse().forEach(function(item) {
// console.log(item);
// })
}
// if you were to just return the reversed array you would have to assign it to a variable
// this is just an example and wouldnt technically work because arrayOne is empty
// also if you use this YOU HAVE TO RETURN THE ARRAY COPY
// const reversedArray = printReverse(arrayOne);
If you want it to return something, you have to add a return within the function, such that
function printReverse(arrayOne) {
for(var i = arrayOne.length-1; i < 0; i--) {
console.log(arrayOne[i]);
}
return "";
}
However, in your case, this doesn't make a lot of sense. You can only return one thing, be it a String, int, array, object, whatever. But once your program hits one return statement, it will quit the function after returning the value.

Query regarding arrays

var arr=[1,2,3,[4,5],6,[7,8,9]],x,j;
for(x in arr)
for(j in arr[x])
console.log(arr[x][j]);
I want to print 1,2,3,...,9 but the above code prints 4,5,7,8,9.
I think "join" is enough:
console.log(arr.join());
If i understand your question correctly, you want to console to log 1 through 9. The way you currently have it, its only going to print the arrays within your array - that is why you are only getting 4,5,7,8,9.
What you could do is check to see if the value is an array in your first loop - if it is, iterate over it and print the values. If it is not, simply print the value.
if(arr[x].constructor === Array) {
//loop over the array and print out the values
for (j in arr[x]) {
console.log(arr[x][j])
}
} else {
//print out the plain value
console.log(arr[x])
}
Here is a pen to show the result: http://codepen.io/kyledodge/pen/zGwPBo
Another option is to use recursion. You could do something like this:
var printArrayValue = function(array) {
for (var i = 0; i < array.length; i++) {
if (array[i].constructor === Array) {
//if this an array, call this function again with the value
printArrayValue(array[i]);
} else {
//print the value
console.log(array[i]);
}
}
}
printArrayValue(arr);
Here is a pen to show the result: http://codepen.io/kyledodge/pen/VLbrPX
Coerce each element to array:
var arr=[1,2,3,[4,5],6,[7,8,9]],x,j;
for(x in arr) {
var arr2 = [].concat(arr[x]);
^^^^^^^^^^^^^^^^^
for(j in arr2)
console.log(arr2[j]);
}
This works because concat takes either an array or a single value.

Find duplicate object values in an array and merge them - JAVASCRIPT

I have an array of objects which contain certain duplicate properties: Following is the array sample:
var jsonData = [{x:12, machine1: 7}, {x:15, machine2:7},{x:12, machine2: 8}];
So what i need is to merge the objects with same values of x like the following array:
var jsonData = [{x:12, machine1:7, machine2:8}, {x:15, machine2:7}]
I like the lodash library.
https://lodash.com/docs#groupBy
_.groupBy(jsonData, 'x') produces:
12: [ {x=12, machine1=7}, {x=12, machine2=8} ],
15: [ {x=15, machine2=7} ]
your desired result is achieved like this:
var jsonData = [{x:12, machine1: 7}, {x:15, machine2:7},{x:12, machine2: 8}];
var groupedByX = _.groupBy(jsonData, 'x');
var result = [];
_.forEach(groupedByX, function(value, key){
var obj = {};
for(var i=0; i<value.length; i++) {
_.defaults(obj, value[i]);
}
result.push(obj);
});
I'm not sure if you're looking for pure JavaScript, but if you are, here's one solution. It's a bit heavy on nesting, but it gets the job done.
// Loop through all objects in the array
for (var i = 0; i < jsonData.length; i++) {
// Loop through all of the objects beyond i
// Don't increment automatically; we will do this later
for (var j = i+1; j < jsonData.length; ) {
// Check if our x values are a match
if (jsonData[i].x == jsonData[j].x) {
// Loop through all of the keys in our matching object
for (var key in jsonData[j]) {
// Ensure the key actually belongs to the object
// This is to avoid any prototype inheritance problems
if (jsonData[j].hasOwnProperty(key)) {
// Copy over the values to the first object
// Note this will overwrite any values if the key already exists!
jsonData[i][key] = jsonData[j][key];
}
}
// After copying the matching object, delete it from the array
// By deleting this object, the "next" object in the array moves back one
// Therefore it will be what j is prior to being incremented
// This is why we don't automatically increment
jsonData.splice(j, 1);
} else {
// If there's no match, increment to the next object to check
j++;
}
}
}
Note there is no defensive code in this sample; you probably want to add a few checks to make sure the data you have is formatted correctly before passing it along.
Also keep in mind that you might have to decide how to handle instances where two keys overlap but do not match (e.g. two objects both having machine1, but one with the value of 5 and the other with the value of 9). As is, whatever object comes later in the array will take precedence.
const mergeUnique = (list, $M = new Map(), id) => {
list.map(e => $M.has(e[id]) ? $M.set(e[id], { ...e, ...$M.get(e[id]) }) : $M.set(e[id], e));
return Array.from($M.values());
};
id would be x in your case
i created a jsperf with email as identifier: https://jsperf.com/mergeobjectswithmap/
it's a lot faster :)

Javascript - Do something when an element in two arrays are the same?

I found a solution to where I get returned an array of elements without duplicates:
Array1 = Array1.filter(function(val) {
return Array2.indexOf(val) == -1;
});
However, I want to modify this code just a little bit. Instead of being returned an array without duplicates, I want to do something when there is a duplicate. The problem is, I'm not sure how exactly this code works. The thing is I'm not sure how val gets set, or what it even is.
for (var i = 0; i < json.length; i++) {
var item = json[i];
// if json.indexOf(val?), do something
}
Read the docs for the Array filter method then. The val parameter of the callback will be passed the single array items, i.e. json[i] or item in your case:
for (var i = 0; i < json.length; i++) {
var item = json[i];
if (json.indexOf(item) >= 0) {
// do something
}
}
var newArray = array1.filter(function(v, i) {
return array1.indexOf(v) == i;
});
This will return only unique itesm from array1;
array1.filter(function(v, i) {
// write your code here ('v' is individual value and 'i' is its index)
// don't return any anything if you don't want unique array to be returned.
// 'array1.indexOf(v) == i' checks if current value is duplicate from previous any values.
// try putting console.log on values you don't understand like (console.log(v,i) for values of 'v' and 'i')
return array1.indexOf(v) == i;
});
and off-curse you can loop an array with for loop as
for(i in array1){
// where i is index of array1, to get current value use array1[i]
if(array2.indexOf(array1[i]) >= 0){
// do something
}
console.log(i);
}
val is set by Array.prototype.filter, which calls the callback function on each element in the array. Since you don't want to filter you can use Array.prototype.forEach instead, which also calls the callback function once for each element in the array:
Array1.forEach(
// This function is called once per element in Array1
function(val){
if(Array2.indexOf(val) != -1){ // Check if that element is also in Array2
// `val` is in both arrays,
// Do something with it
}
}
);
You can utilize some modern libraries... like underscorejs.
Intersection is what you're looking for i guess: http://underscorejs.org/#intersection
_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
=> [1, 2]
So your code may be something like
if(_.insersection(arr1, arr2)){
//since [] array is Falsy in JS this will work as a charm
}
From MDN: indexOf
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
From MDN: filter
Creates a new array with all elements that pass the test implemented by the provided function.
The first function works by returning true when an item from array1 isn't found in array2 (== -1). i.e.: Iterate through A and add anything not found in B.
So, to change to return only duplicates return true for anything that is found in both:
Array1 = Array1.filter(function(val) {
return Array2.indexOf(val) >= 0;
});
Array1 now contains only items with duplicates.

jQuery/Javascript if value is in array, continue

I have data that is in an array, these are the ID's for users that have commented on said post. I want to compare this array with the id of the user, and if their id is in this array, continue with the code.
my array looks like:
({0:"1", 3:"6"}) // 1 and 6 are the ID's that I want to work with.
So I want to do something like:
var array = ({0:"1", 3:"6"});
var userID = 6;
if(in(array)==userID)
{
///you are in the list, so do whatever
}
Instancing your array like that will not create an array, but an object. Normally, you instantiate arrays in javascript like this:
var arr = [17, 4711];
Checking for a value using Array.indexOf:
arr.indexOf(17); // => 0
arr.indexOf(4711); // => 1
arr.indexOf(42); // => -1
Pushing:
arr.push(42);
arr.indexOf(42); // => 2
Array.indexOf is not in IE < 9, so I suggest you look into using a shim.
function inArray(needle, haystack) {
var count = 0;
for (var k in haystack) {
if (haystack.hasOwnProperty(k)) {
++count;
}
}
for (var i in haystack) {
if(haystack[i] == needle) return true;
}
return false;
}
​
See : http://jsfiddle.net/ryN6U/1/
If will not work with your object :)
You can loop through your object and check it against your userid. Something like
$(document).ready(function(){
var myArray = ({0:"1", 3:"6"});
var userId = 6;
for(vals in myArray){
if(userId == myArray[vals]){
alert("userid exists in array");
}
}
});
When testing against an array I would use jQuery's inArray()
if your looking for the first item in an object with a certain value look at this thread
json index of property value

Categories