Combining Array values

  • Thread starter Michael Haufe (\TNO\)
  • Start date
M

Michael Haufe (\TNO\)

I'm trying to combine certain values in an array without resorting to
loops, but I can't get it to come out right (My diet is lacking in
Omega-3 or something today), So I'm hoping someone has a fresh
perspective on my problem:

var arr = [1, 2, 3, "a", "b", "c", 4, 5, 6];

var result = arr.reduce(function(a,b){
//how would you approach this?
});

// result should equal [6, "a", "b", "c", 15]
 
R

rf

Michael Haufe ("TNO") said:
I'm trying to combine certain values in an array without resorting to
loops, but I can't get it to come out right (My diet is lacking in
Omega-3 or something today), So I'm hoping someone has a fresh
perspective on my problem:

var arr = [1, 2, 3, "a", "b", "c", 4, 5, 6];

var result = arr.reduce(function(a,b){
//how would you approach this?
});

// result should equal [6, "a", "b", "c", 15]

Why exactly should the result equal the above? I can't see any reason it
should.

And what is wrong with a loop?
 
R

RobG

I'm trying to combine certain values in an array without resorting to
loops, but I can't get it to come out right (My diet is lacking in
Omega-3 or something today), So I'm hoping someone has a fresh
perspective on my problem:

var arr = [1, 2, 3, "a", "b", "c", 4, 5, 6];

var result = arr.reduce(function(a,b){
//how would you approach this?

});

// result should equal [6, "a", "b", "c", 15]

What reduce function are you referring to? Presumably the one
introduced in JavaScript 1.8. If it's something else, say so.

You also need to explain the logic you wish to implement. Giving input
and desired output is only half the job, it is left up to the reader
to work out what the logic is and they may well get it wrong.

Some questions:

1. Should the original array be modified, or left intact?

2. What are the parameters (a, b) being passed to the anonymous
function?

3. What is the logic? As far as I can see, it's "replace sequences of
numbers with their arithmetic sum" or is the function required to be
more generic?
 
M

Michael Haufe (\TNO\)

Michael Haufe ("TNO") said:
// result should equal [6, "a", "b", "c", 15]

Why exactly should the result equal the above? I can't see any reason it
should.

Because that's the goal. The desire is to combine contiguous numeric
values in the array into single values by taking their sum.
And what is wrong with a loop?

A functional approach is cleaner since it consumes fewer lines and
doesn't require one or more free variables. In the context of my use,
code efficiency is irrelevant.
 
M

Michael Haufe (\TNO\)

What reduce function are you referring to? Presumably the one
introduced in JavaScript 1.8. If it's something else, say so.

Yes, that's correct:
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/Reduce
1. Should the original array be modified, or left intact?

A new array should be created.
2. What are the parameters (a, b) being passed to the anonymous
function?

The signature is defined in the above link:
[].reduce(function(previousValue, currentValue, index, array)
{...},initialValue)

3. What is the logic? As far as I can see, it's "replace sequences of
numbers with their arithmetic sum" or is the function required to be
more generic?

Correct. Combine contiguous numeric values in the array into single
values by taking their sum.
 
R

RobG

What reduce function are you referring to? Presumably the one
introduced in JavaScript 1.8. If it's something else, say so.

Yes, that's correct:https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Object...
1. Should the original array be modified, or left intact?

A new array should be created.
2. What are the parameters (a, b) being passed to the anonymous
function?

The signature is defined in the above link:
[].reduce(function(previousValue, currentValue, index, array)
{...},initialValue)
3. What is the logic? As far as I can see, it's "replace sequences of
numbers with their arithmetic sum" or is the function required to be
more generic?

Correct. Combine contiguous numeric values in the array into single
values by taking their sum.

I guess you mean the first part of the question is correct. Here's a
full example, I've added alerts to show what's happening. It can be
written in less code, but that is not a good reason of itself to do
that.

The function should probably be defined separately to make maintenance
a bit easier. Note that the initial value is an empty array [], and
that the function works if the original array starts with a number or
string value.


var arr = [1, 2, 3, 'a', 'b', 'c', 4, 5, 6];
var result = arr.reduce(function(a, b) {
var i = a.length;

if (i && typeof b == 'number') {

if (typeof a[i-1] == 'number') {
i--;
a = a + b;
alert('number - i: ' + i + '\n' + a + '\n' + b);

} else {
a = b;
alert('alpha - i: ' + i + '\n' + a + '\n' + b);
}

} else {
a = b;
alert('alpha - i: ' + i + '\n' + a + '\n' + b);
}

return a;

}, []);

alert(arr + '\n' + result);
 
M

Michael Haufe (\TNO\)

I guess you mean the first part of the question is correct. Here's a
full example, I've added alerts to show what's happening. It can be
written in less code, but that is not a good reason of itself to do
that.

...

Thanks, I had actually come up with the following not too long ago:

var arr = [1, 2, 3, "a", "b", "c", 4, 5, 6];
var result = arr.reduce(function(a,b){
if(!a)
return ;
if(typeof b === "number"){
if(typeof a[a.length - 1] === "number")
a[a.length - 1] += b;
else
a[a.length] = b;
} else
a[a.length] = b;
return a;
},[]);
 
R

RobG

I guess you mean the first part of the question is correct. Here's a
full example, I've added alerts to show what's happening. It can be
written in less code, but that is not a good reason of itself to do
that.

Thanks, I had actually come up with the following not too long ago:

var arr = [1, 2, 3, "a", "b", "c", 4, 5, 6];
var result = arr.reduce(function(a,b){
    if(!a)
        return ;


Think about that a little more. Why would the condition return true?

    if(typeof b === "number"){
        if(typeof a[a.length - 1] === "number")
            a[a.length - 1] += b;
        else
            a[a.length] = b;
    } else
        a[a.length] = b;
    return a;

},[]);
 
A

abozhilov

Michael said:
I'm trying to combine certain values in an array without resorting to
loops, but I can't get it to come out right (My diet is lacking in
Omega-3 or something today), So I'm hoping someone has a fresh
perspective on my problem:

What will be result if array contains properties which values:


var arr = [NaN, Infinity, -Infinity];

NaN and Infinity ares primitive values of Number Type.

And, where is wrong in explicit loop algorithm?
 
M

Michael Haufe (\TNO\)

What will be result if array contains properties which values:

var arr = [NaN, Infinity, -Infinity];

NaN and Infinity ares primitive values of Number Type.

What I posted was an off the cuff example for easier communication.
I'm using this on an array of constructor instances, not numbers so
the problem posed is irrelevant. The general approach RobG provided
would still have application.
And, where is wrong in explicit loop algorithm?

A loop requires one or more free variables and consumes more lines of
code. With arrays especially, using methods are cleaner and offer you
the option of chainability and code re-use, as a result this leads to
shorter code and easier maintenance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top