/    Sign up×
Community /Pin to ProfileBookmark

newArray.push is not a function????

In order to match up and resort items between two arrays and come up with a new array,I wrote the following code. y is a subset of x: x being the array that has the sorting order I want to emulate and y being the array that has the items that I want in the new array:

function createArray (x,y) {
var newArray = new Array();
var thisStr = y.join();
var thisReg;
var chckitem;

for(i=0;i<x.length;i++){
chckitem = x[i];
thisReg = new RegExp(chckitem);
if (thisReg.test(thisStr)) {
newArray.push(chckitem);
}
}
return newArray;

This works great as long as there are no items in x that may be a substring of items in y (oops!)…

So, in order to fix this problem (thanks to KOR for pointing out the solution) I used code as such:

function createArray (x,y) {
var newArray = new Array();
var chckitem;
for(i=0;i<x.length;i++){
chckitem=x[i];
for(f=0;f<y.length;f++) {
if(y[f]==chckitem){
newArray.push(chckitem);
}
}
}
return newArray;
}

Now, however, whenever I call this function, I get ‘newArray.push is not a function’.

WHat’s up?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanOct 19.2004 — The code seemed to work fine for me. I just ran a test and it returned a value with no error.


RyanJ
Copy linkTweet thisAlerts:
@suegriffauthorOct 19.2004 — [i]Originally posted by sciguyryan [/i]

[B]The code seemed to work fune for me. I just ran a test and it returned a value with no error.

[/B]
[/QUOTE]


arrgh. that figures. As I haven't changed anything (such as when and where the function is called) except the function that means I must have some weird conflict going on with the new code.....
Copy linkTweet thisAlerts:
@sciguyryanOct 19.2004 — [i]Originally posted by suegriff [/i]

[B]arrgh. that figures. As I haven't changed anything (such as when and where the function is called) except the function that means I must have some weird conflict going on with the new code..... [/B][/QUOTE]



Sounds like a reasonable conclusion.

RyanJ
×

Success!

Help @suegriff spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...