Javascript Array Concat not working. Why?
The concat method doesn't change the original array, you need to reassign it.
if ( ref instanceof Array )
this.refs = this.refs.concat( ref );
else
this.refs.push( ref );
concat does not join JavaScript arrays together?
.concat
creates a new Array. You need to overwrite the old one.
scriptUrls = scriptUrls.concat(urls);
Or if you want to keep the original
scriptUrls
Array, you can .push()
the values in.scriptUrls.push.apply(scriptUrls, urls);
This uses .apply()
to convert urls
into individual arguments passed to .push()
. This way the content of urls
is added to scriptUrls
as individual items.Also, note that
.concat()
flattens the Array. If you wanted an Array of Arrays, then you'd use scriptUrls.push(urls)
. Why can't I concatenate these two arrays?
One way to use concat would be to do the followingFrom documentation - The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
state.building.panoramas = state.building.panoramas.concat(panoramas)
Or you can simply[].push.apply(state.building.panoramas, panoramas);
Javascript concat not working as expected - not merging
Declare array before for loop, set fixed.innerHtml
after and read about javascript scope.
var fixed = document.getElementById('fixed');
var sections = document.getElementsByClassName('section');
var arr = [];
for (i = 0; i < sections.length; i++) { arr = arr.concat(sections[i]);}
fixed.innerHTML = arr;
<div class="fixed" id="fixed"></div><div class="section"></div><div class="section"></div><div class="section"></div><div class="section"></div><div class="section"></div>
JavaScript concat not working as expected, care to elaborate?
As per documentation of Array.prototype.concat
:
Which means it does not modify the object it is applied to.Returns a new array comprised of this array joined with other array(s)
and/or value(s).
Change to:
allCities = allCities.concat(cities[c]);
javascript recursive function concat not working
Array.prototype.concat()
So those lines do nothing:The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
if("sp" in val) values.concat(getValues(val.sp))
else if("it" in val) values.concat(getValues(val.it))
You need to write:if("sp" in val) values = values.concat(getValues(val.sp))
else if("it" in val) values = values.concat(getValues(val.it))
And you should not use map
if you don't use it's result. Use forEach
instead. Why is Array.push() throwing an error but Array.concat() does not?
.concat
returns a new array with both the current items and the new ones where .push
modifies the current array instead of returning a new one.
If that array was initially loaded from vuex it results in an unwanted mutation of the store.
Edit:
In the store
mutations: {
addFile(state, file) {
state.ItemData[0].Files.push(file);
},
}
In the componentNewFiles.forEach(file => store.commit("addFile", file));
Related Topics
How to Call a Dynamically-Named Method in JavaScript
Cancel/Kill Window.Settimeout() Before It Happens
How to Catch Jquery $.Getjson (Or $.Ajax with Datatype Set to 'JSONp') Error When Using JSONp
Using the Haversine Formula in JavaScript
External Resource Not Being Loaded by Angularjs
How to Getting Browser Current Locale Preference Using JavaScript
Printing a Web Page Using Just Url and Without Opening New Window
Skipping Optional Function Parameters in JavaScript
How to Replace Last Occurrence of Characters in a String Using JavaScript
How to Execute a Dynamically Loaded JavaScript Block
Uncaught Typeerror: Cannot Use 'In' Operator to Search for 'Length' In
How to Convert Numbers Between Different Bases in JavaScript
How to Access Nested JSON Data
How to Pass JavaScript Variables as Parameters to Jsf Action Method
Omitting the Second Expression When Using the If-Else Shorthand