0

I have an array like var myArr = [0,-1,2,-3,4,-5,6];

I want to separate all positive No's and negative No's in array format like

positiveArr = [0,2,4,6] and negativeArr = [-1,-3,-5].

Then like to print both arrays in object format.

Final expected result something like an object:

{Positive: [0,2,4,6], Negative: [-1,-3,-5]}

Thanks.

3
  • 3
    which languages? what did you do so far? Commented Aug 29, 2018 at 11:47
  • 1
    Write into console -> Computer I command: Split myArr = [0,-1,2,-3,4,-5,6]; into Positive numbers and Negative numbers. Disobedience will be punished by formating the hard drive. And if it does not work, format all disks, otherwise the computer will be laughing at you. Commented Aug 29, 2018 at 11:55
  • Does the order matter? Is it okay to get, say, [-5, -3, -1] for the Negative array? Commented Aug 30, 2018 at 13:40

4 Answers 4

2

You can do it with the help of Array.prototype.reduce:

const myArr = [0,-1,2,-3,4,-5,6];

const result = myArr.reduce((all, item) => {

    const key = item >= 0 ? 'Positive' : 'Negative';

    all[key].push(item);

    return all;

}, {Positive: [], Negative: []});

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

very smart solution, +1. The logic of the initial value very ingenious
1

As far as I see your array definition, you are using JavaScript - don't forget to add the tag javascript.

You need the following output:

var obj = {positive: new Array(), negative: new Array()};

Iterate once through all the elements and add them to the correct array:

var myArr = [0,-1,2,-3,4,-5,6];
var obj = {positive: new Array(), negative: new Array()};
myArr.forEach(i => { 
    if (i>=0) { obj.positive.push(i) } else { obj.negative.push(i); }
});
console.log(JSON.stringify(obj));

The result obj will have the following arrays:

negative : (3) [-1, -3, -5]
positive : (4) [0, 2, 4, 6]

Printed out with JSON.stringify(obj) will result in:

{"positive":[0,2,4,6],"negative":[-1,-3,-5]}

2 Comments

Hi Denis. Thank you. Your output shows both array list separately. I would like to print exactly { Positive: [0, 2, 4, 6], Negative: [-1, -3, -5] } . I mean both array lists separated by comma inside curly braces.
What have you tried so far? Use ob the obj the following: JSON.stringify(obj). See my edited answer.
0

If you need to do it in JavaScript, you can do so

let myArr = [0,-1,2,-3,4,-5,6];
let obj = {Positive: myArr.filter(e => e < 0), Negative: myArr.filter(e => e >= 0)};
console.log(obj);

4 Comments

I don't recommend to iterate through array twice.
@Nikolas Why? filter function makes new array, so I don't see any problem
Yes, it returns an array. But you call this function twice and it iterates the entire array.
So, you mean perfomance, I absolutely agree, but in such cases it doesn't matter, both solutions are good but my shorter :)
0

You can be explicit, and is totally find

const numbers = [0, -1, 2, -3, 4, -5, 6];
const positives = numbers.filter(number => number >= 0);
const negatives = numbers.filter(number => number < 0)
const results = {positives: positives, negatives: negatives}

console.log(results); // {positives: [0, 2, 4 ,6], negatives: [-1, -3, -5]}

I attached an executable example

const numbers = [0, -1 , 2, -3, 4, -5, 6];
const positives = numbers.filter(number => number >= 0);
const negatives = numbers.filter(number => number < 0);
const results = {positives: positives, negatives: negatives};

console.log(results);

5 Comments

It does not give the output as expected. It gives [object Object] instead of { positive: [0,2,4,6], negative: [-1,-3,-5] } where is the wrong? I checked in my system. Thanks
@user615274: I don't recommend to iterate through array twice.
Hi Bhagabat, I updated the answer with an runnable snippet
I agree Nikolas, in honor of the truth at the time I did not think of a more compact solution like the one presented by Leonid Pyrlia. On the other hand I think sometimes new users appreciate a solution that is a bit easier to understand
I am learning JavaScript. I am new to this community. I am happy for the quick response for my query. I really thankful to all and this community. I need your support to learn this language. Any suggestion for me is most welcome. How to learn and master this quickly? Once again thanks a lot everyone.

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.