Changing one char in an array to another java [duplicate] - javascript

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 2 years ago.
It must be super simple, but I'm completely stuck.
var arr = ["ABCD", "asda12"];
var a = arr[0];
a[1] = 'S';
console.log(a[1]);
It logs "B" and I expect "S", why can't I do it this way?

In your code, "a" is a string and you can't change it's value in that way. You can try setting "a" as an array, then you can change it with the way you do:
var arr = ["ABCD", "asda12"];
var a = [...arr[0]];
a[1] = 'S';
console.log(a[1]);

Related

How to compare two array and mismatch based on index using javascript? [duplicate]

This question already has answers here:
How to compare two arrays and then return the index of the difference?
(4 answers)
How to get the difference between two arrays in JavaScript?
(84 answers)
Javascript compare 2 arrays index by index, and not by total number of values the 2 arrays have in common
(2 answers)
Closed 2 months ago.
var a=["a","b","c","d"];
var b=["b","a","c","d"];
I need output like:
mismatched array from a
mismatch array=["a", "b"];
You can use filter function and check value with the index in both the array and then you can get the mismatched values from a.
var a = ["a", "b", "c", "d"];
var b = ["b", "a", "c", "d"];
var c = a.filter(function (item, index) {
return item !== b[index];
});
console.log(c);
Here I used Javascript's filter method to get the mismatched Array.
const a = ["a","b","c","d"];
const b = ["b","a","c","d"];
const mismatchedArr = a.filter((aItem,index) => aItem !== b[index]);
console.log(mismatchedArr); //Prints ["a","b"]

Remove duplicate content in array from another array [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 4 months ago.
In Javascript, I have this
var a = ["1","2","3"]
var b = ["3","4","5"]
assuming "b" reads "a" and removes the 3 because it is repeated, how can I get this?
var c = ["4","5"]
Thank You!
You can use set and spread operators.
set has property to have all unique value object.
Below we are using Set constructor passing values using spread operator and it return an object so to get an array we are again passing that object with spread operator.
let a = ["1", "2", "3"]
let b = ["3", "4", "5"]
let c = [...new Set([...a, ...b])];
console.log(c);
Check if value exists on another array using includes()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
You can remove values by using filter()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
To do what you want:
const a = ["1","2","3"]
const b = ["3","4","5"]
const c = b.filter((value) => !a.includes(value))
This has been answered before and is in detail: How to get the difference between two arrays in JavaScript?

Remove multiple positions in array [duplicate]

This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 3 years ago.
I have the following array
myArray = ["zell", "allen", 34, 223344, age , "Stree 45"]
i need to delete every position which be length less than 4
in this case the position 2 and 4
I wrote
for(var i=0; i<myArray.length; i++){
if(myArray[i].trim().length<3){
myArray.splice(i,1);
}
}
but works only with the first one, I need with every one
thanks
You can filter out the items that are shorter then 4 chars:
var myArray = ["zell", "allen", 34, 223344, "age" , "Stree 45"];
var filteredArr = myArray.filter(item => item.toString().length >= 4);
console.log(filteredArr);

Why "123".split('') is not equal to [1, 2, 3].join('').split('') [duplicate]

This question already has answers here:
Why is [] !== [] in JavaScript? [duplicate]
(3 answers)
How to check if two arrays are equal with JavaScript? [duplicate]
(16 answers)
Closed 4 years ago.
This appears as a very basic question, but couldn't find any explanations on SO.
Consider this:
var arr = [1, 2, 3];
var str = "123";
function compare(){
return arr.join('').split('') === str.split('')
}
console.log(compare());
console.log(arr.join('').split(''))
console.log(str.split(''))
Cant understand why console logs false...?
Because although the two arrays you're comparing are equivalent, they are not the same array. == (and ===) with objects checks to see if they're the same object.
What you have, in effect, is:
console.log(["1", "2", "3"] == ["1", "2", "3"]);
You compare two objects' references, not their content. Because you have 2 different objects, their references are not equal.
=== with reference types will return true if 2 variables refer to the same object. In the below example both a and b refer to the same object (same part in the memory), so they are equal.
const a = {};
const b = a; // assign `a`'s value, which is a reference to the `b`
console.log(a === b);
They are not same array & they are separate object reference. If you want to compare these two , then stringify them
var arr = [1, 2, 3];
var str = "123";
function compare() {
return (arr.join('').split('')).toString() === str.split('').toString()
}
console.log(compare());
console.log(arr.join('').split(''))
console.log(str.split(''))

Create a simple TWO dimensional array in JavaScript. I am not just trying to append value in an array [duplicate]

This question already has answers here:
How to append something to an array?
(30 answers)
Closed 5 years ago.
var a = ["1", "2"];
var b = ["a", "b"];
var c = [];
//Can someone suggest how do i push a and b into c such that c is a 2-dimensional array? (By using JavaScript)
//c should look like [["1","2"],["a","b"]]
You are asking a very basic question, the answer is:
var a = ["1", "2"];
var b = ["a", "b"];
var c = []; // you could have written c = [a, b];
c.push(a, b);
It's quite simple. Just use push function and pass the array as an argument.
c.push(a);
c.push(b);
or push them together like: c.push(a,b);

Categories

Resources