0

I have a problem with splitting the array into to separate arrays Odd and Even. What I did was:

componentDidMount() {
 axios.get( `${this.props.cmsUrl}types/genres`)
 .then(response => { if ( response.data !== undefined ) {

  let even = [];
  let odd = [];
  let allData = ["a", "b", "c", "d", "e", "f", "g", "h", ];

  for (var i = 0; i < allData.length; ++i) {
    if ( ( allData[i] % 2 ) === 0) { 
      even.push(allData[i]);
    } 
    else { 
      odd.push(allData[i]); 
    }
  };
  console.log("allData : ",allData);
  console.log("even : ",even);
  console.log("even : ",odd);
 }}
)
}

What I really was expecting is

allData = [a, b, c, d]
odd = [a , c]
even = [b, d]

But what really happened is

allData = even 
odd = empty array

is this my problem => ( allData[i] % 2 ) === 1

The console.log is :

  allData :  (8) ["a", "b", "c", "d", "e", "f", "g", "h"]0: "a"1: "b"2: "c"3: 
  "d"4: "e"5: "f"6: "g"7: "h"length: 8__proto__: Array(0)
    details.js:56 even :  []length: 0__proto__: Array(0)
    details.js:57 odd :  (8) ["a", "b", "c", "d", "e", "f", "g", "h"]0: 
   "a"1: "b"2: "c"3: "d"4: "e"5: "f"6: "g"7: "h"length: 8__proto__: Array(0)

Thanks

4
  • Cannot reproduce. jsfiddle.net/drs8opk7 Commented Sep 4, 2018 at 7:28
  • can u log response.data and put results in your question ? Commented Sep 4, 2018 at 7:34
  • Mr Soroush_Neshat : This is the console log allData : (8) ["a", "b", "c", "d", "e", "f", "g", "h"]0: "a"1: "b"2: "c"3: "d"4: "e"5: "f"6: "g"7: "h"length: 8__proto__: Array(0) details.js:56 even : []length: 0__proto__: Array(0) details.js:57 odd : (8) ["a", "b", "c", "d", "e", "f", "g", "h"]0: "a"1: "b"2: "c"3: "d"4: "e"5: "f"6: "g"7: "h"length: 8__proto__: Array(0) Commented Sep 4, 2018 at 7:38
  • @Ranny tnx for infos . i posted the answer to solve your problem Commented Sep 4, 2018 at 7:49

5 Answers 5

2

You've to use charCodeAt method because allData is an array of string not array of numbers.

It's the solution of your problem:

if ( ( allData[i].charCodeAt(0) % 2 ) === 0) { your code }

=================

as Thomas Scheffer mentioned it seems u want to sort based on character index not its value .

so if u want to sort based on index u have to write :

if ( ( i % 2 ) === 0) { your code }

it converts like this :

allData = ["b","f","z","w"] => { odd=["f","w"], even=["b","z"]  }

but if u want to sort based on character value u have to write :

if ( ( allData[i].charCodeAt(0) % 2 ) === 0) { your code }

it converts like this :

allData = ["b","f","z","w"] => { odd=["w"] , even=["b","f","z"] }
Sign up to request clarification or add additional context in comments.

9 Comments

Or just use if( ( i % 2 ) === 0 ){ } as its about the index number not really the data in allData.
@Soroush_Neshat Thaaaaaaaaaank you very much it is working now
@ThomasScheffer if characters are shuffled u cant do that anymore
@Soroush_Neshat charCodeAt() returns the ASCII code for the first character in that value. Not sure if thats what @Ranny was asking for.
@Ranny Just remember that Arrays start at 0, so this will skew your expected results. As "a" would be in even instead of odd.
|
1

instead of for loops you can use filter method

/*ES6*/

let evenArray = result.filter((a,i) => i%2);
let oddArray = result.filter((a,i) => !(i%2));

/*ES5*/

let evenArray = result.filter(function(a,i){return i%2});
let oddArray = result.filter(function(a,i){return !(i%2)});

Comments

0

Right condition is if ( ( allData[i] % 2 ) === 0)

Try in console:

4 % 2
4 % 3

3 Comments

That flip my empty array to be same allData and second one is empty
Can you print your allData array?
if i made : if ( ( allData[i] % 2 ) === 0) and else if ( ( allData[i] % 2 ) === 1) both arrays are empty now
0

You can use Array.prototype.reduce

let allData = [ "a", "b", "c", "d", "e", "f", "g", "h" ];

const { even, odd } = allData.reduce((acc, val, index) => {
    const key = index % 2 ? 'odd' : 'even';
    return {
       ...acc,
       [key]: acc[key].concat(val),
    };
}, { odd: [], even: [] });

console.log( 'even', even );
console.log( 'odd', odd );

Comments

0

let allData = ["a", "b", "c", "d", "e", "f", "g", "h", ];

let even = [];
let odd = [];

for (let i = 0; i < allData.length; i++) {
if (i % 2 == 0) {
    even.push(allData[i]);
} else {
    odd.push(allData[i]);
}
}

console.log("Even array is : " + even);
console.log("Even array is : " + odd);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.