0

So I have a array of items which looks like this:

var receivedQuery = [   { 'ingredients.ingredient': /^what$/i },
                        { 'ingredients.ingredient': /^you$/i},
                        { 'ingredients.ingredient': /^said$/i},
                        { 'ingredients.ingredient': /^about$/i},
                        { 'ingredients.ingredient': /^that$/i}
                    ];

and I'm trying to log in console each item in array , and the rest of array without that current item in it with for loop. And I tried to do so with splice method like this:

var splicedQuery = receivedQuery;

for (i = 0, max = receivedQuery.length; i < max; i++) {
    var position = i;
    splicedQuery = splicedQuery.splice(position, 1);
    console.log( receivedQuery[i], splicedQuery );

};

but I'm not receiving it as I wanned to:

{ 'ingredients.ingredient': /^you$/i } [ { 'ingredients.ingredient': /^what$/i } ]
{ 'ingredients.ingredient': /^said$/i } []
{ 'ingredients.ingredient': /^about$/i } []
{ 'ingredients.ingredient': /^that$/i } []
undefined []

and I want it to output something like this:

{ 'ingredients.ingredient': /^what$/i }, [ { 'ingredients.ingredient': /^you$/i}, { 'ingredients.ingredient': /^said$/i}, { 'ingredients.ingredient': /^about$/i}, { 'ingredients.ingredient': /^that$/i} ]
{ 'ingredients.ingredient': /^you$/i }, [ { 'ingredients.ingredient': /^what$/i}, { 'ingredients.ingredient': /^said$/i}, { 'ingredients.ingredient': /^about$/i}, { 'ingredients.ingredient': /^that$/i} ]
{ 'ingredients.ingredient': /^said$/i }, [ { 'ingredients.ingredient': /^what$/i}, { 'ingredients.ingredient': /^you$/i}, { 'ingredients.ingredient': /^about$/i}, { 'ingredients.ingredient': /^that$/i} ]
........

I'm not sure exactly how to do it do console it it right way... what is the best method? Maybe to use something else than splice() or?

You can see and edit my situation in jsfiddle to: http://jsfiddle.net/RgGzE/

dzordz
  • 2,277
  • 13
  • 51
  • 74

3 Answers3

1

That is because splice returns an array containing the removed elements.

var splicedQuery = receivedQuery.slice();

for (var i = 0, max = splicedQuery.length; i < max; i++) {
    console.log(splicedQuery.splice(0, 1), splicedQuery);
};

Using slice will create a shallow copy of the receivedQuery array.

Working example: http://jsfiddle.net/VUetk/1/

CD..
  • 72,281
  • 25
  • 154
  • 163
  • yea the logic is wrong I know that...I'm still trying to develop right logic for it... – dzordz Feb 15 '14 at 18:26
  • At least in Chrome, it still doesn't log the correct value, because of http://stackoverflow.com/q/4057440/218196. – Felix Kling Feb 15 '14 at 18:36
  • that is something close to the what I wanted except it does not show again removed item in previous loop, it removes item by item from array, in the end recievedQuery is empty – dzordz Feb 15 '14 at 18:36
  • So you can use `slice` to have a shallow copy of your array. – CD.. Feb 15 '14 at 18:39
0

If you want to show the current element, and what the whole array looks like without the current element, you'd have to do something like this

for (i = 0, max = receivedQuery.length; i < max; i++) {
    var copy = receivedQuery.slice(0);
    console.log(copy.splice(i, 1), copy);
};

Fiddle

You have to make a copy because splice will modify the array you pass in. We want the original one intact.

Anid Monsur
  • 4,538
  • 1
  • 17
  • 24
  • Why can't you just do `console.log(receivedQuery.slice(i, 1), receivedQuery);` – megawac Feb 15 '14 at 18:55
  • @megawac Because on the next pass, `receivedQuery` will be missing the previous element, until there's nothing left in the array. He wants to show the original array every time without the current element, so you need to make a copy – Anid Monsur Feb 15 '14 at 19:02
0

Use filter function :

for (i = 0, max = receivedQuery.length; i < max; i++) {
  var filtered = receivedQuery.filter(function(elem, index) {
    return index != i;
  });
  console.log( receivedQuery[i], filtered );
};
dongseok0
  • 737
  • 2
  • 7
  • 18