This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I have an array, that happens to be filled with arrays: [[1, "thing"], [2, "other thing"], [3, "still other thing"]] I want to be able to select a value inside one of the arrays without having to make the array a temporary variable and then pick the item. Can this be done?
You can index into a nested array with the same '[]' syntax.
var arr = [[1, "thing"], [2, "other thing"], [3, "still other thing"]];
console.log(arr[0]); // [1, "thing"]
console.log(arr[0][0]); // 1
For reference, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
In that link search for two-dimensional
Related
This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Wrong value in console.log [duplicate]
(4 answers)
Closed 25 days ago.
Screenshot of the Output
This is a simplified version of the problem =>
const z = [
[1, 2, 3, 4, 5]
];
console.log(z);
z[0].splice(1, 1);
console.log(z);
Both Console logs output :
Array [ (5) […] ]
0: Array(4) [ 1, 3, 4, … ]
length: 1
How is splice affecting the first console log?
I was making an algorithm then I noticed that even before calling the splicing function my original array which had thousands of items was getting affected meaning that the "History" or "Log" array that I was trying to form always had the same spliced version of the array that i later on tried splicing.
JavaScript's splice() method modifies the original array, meaning that it directly alters the array on which it is called. This is different from some other methods, such as slice(), which create and returns a new array without modifying the original.
const z = [
[1, 2, 3, 4, 5]
];
console.log(z);
//line below will affect the original array because array is
// refference in js
// z[0].splice(1, 1);
//instaed make a new deep copy of your variable z
const z2 = JSON.parse(JSON.stringify(z));
//now make the required changes to z2
z2[0].splice(1, 1);
console.log(z2);
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed last month.
How can I compare elements in an array like if I for example have arr=[1, 2, 3, 4, 4] and I wanna find out if there are any duplicates and remove them.Are there any fuctions that I can use for this?
you can use a set data structure inbuilt into JS
let arr=[1, 2, 3, 4, 4] ;
let output = [...new Set(arr)]
You need to convert the array to a set, A set has unique elements only as opposed to arrays that can have duplicates. To convert to a set
let arr=[1, 2, 3, 4, 4]
let set = new Set(arr);
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 5 months ago.
I have an array with duplicate elements. While trying to remove duplicate elements using hasOwnProperty getting one duplicate element in array rest of duplicate element removed successfully. expexted output = [1, 3, 2, 4, 5, 6, 7] but getting something [1, 3, 2, 3, 4, 5, 6, 7]. I can use different function and remove duplicates but I'm not understanding why element 3 is coming twice.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.hasOwnProperty(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));
Instead of using hasOwnProperty, you can use includes.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.includes(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));
hasOwnProperty checks whether an object contains a given key. The correct way to check if an array contains an element is to use includes:
if(!output.includes(item))
output.push(item)
This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Closed 6 years ago.
I was under the impression that an array of arrays is not allowed in javascript. Is that still true? Is the following object valid?
object: { array: [ [0],
['1', '2']
]
}
Can someone please point me to examples of usage? I've been using arrays of strings as a workaround.
It is allowed. Try this.
var arrayObj = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(arrayObj[0][0]); // first column/first row
console.log(arrayObj);
This question already has answers here:
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 7 years ago.
I would like to insert a string into an array at a specific index. How can I do that?
I tried to use push()
Well, thats pretty easy. Assuming you have an array with 5 objects inside and you want to insert a string at index 2 you can simply use javascripts array splice method:
var array = ['foo', 'bar', 1, 2, 3],
insertAtIndex = 2,
stringToBeInserted = 'someString';
// insert string 'someString' into the array at index 2
array.splice( insertAtIndex, 0, stringToBeInserted );
Your result will be now:
['foo', 'bar', 'someString', 1, 2, 3]
FYI: The push() method you used just adds new items to the end of an array (and returns the new length)