Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32682 > unrolled thread

Copy full array element.

Started byjonas.thornvall@gmail.com
First post2017-03-04 13:55 -0800
Last post2017-03-05 10:40 +0100
Articles 7 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Copy full array element. jonas.thornvall@gmail.com - 2017-03-04 13:55 -0800
    Re: Copy full array element. jonas.thornvall@gmail.com - 2017-03-04 14:02 -0800
      Re: Copy full array element. jonas.thornvall@gmail.com - 2017-03-04 14:28 -0800
        Re: Copy full array element. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-03-04 15:35 -0800
      Re: Copy full array element. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-03-04 23:53 +0100
        Re: Copy full array element. jonas.thornvall@gmail.com - 2017-03-04 15:11 -0800
          Re: Copy full array element. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-03-05 10:40 +0100

#32682 — Copy full array element.

Fromjonas.thornvall@gmail.com
Date2017-03-04 13:55 -0800
SubjectCopy full array element.
Message-ID<980da03d-c451-40b0-80fd-154b3ed88eb2@googlegroups.com>
This is the attributes of my array element 
     arr[m] = {
            nodelinks: [],
            hold: false,
            rposX: 0,
            rposY: 0,
            rwidth: 10,
            rheigth: 10,
            rcolor: "red",
            bcolor: "green",
            print: true
        };

When i remove an element i want to replace it with the last one, and then pop last element.

How do i do that? Well i know howto do it doing each attribute at a time, but is there a way to just update the full array element in one step? 

function reOrderArr(){
  arr[t]=arr[arr.length-1];
  arr.pop();
}

[toc] | [next] | [standalone]


#32683

Fromjonas.thornvall@gmail.com
Date2017-03-04 14:02 -0800
Message-ID<bc338550-9213-4493-921a-3818db7e4180@googlegroups.com>
In reply to#32682
Den lördag 4 mars 2017 kl. 22:55:28 UTC+1 skrev jonas.t...@gmail.com:
> This is the attributes of my array element 
>      arr[m] = {
>             nodelinks: [],
>             hold: false,
>             rposX: 0,
>             rposY: 0,
>             rwidth: 10,
>             rheigth: 10,
>             rcolor: "red",
>             bcolor: "green",
>             print: true
>         };
> 
> When i remove an element i want to replace it with the last one, and then pop last element.
> 
> How do i do that? Well i know howto do it doing each attribute at a time, but is there a way to just update the full array element in one step? 
> 
> function reOrderArr(){
>   arr[t]=arr[arr.length-1];
>   arr.pop();
> }

In the case above arr.pop(); will not remove any element, nor will it do the copy. Array size stay the same i get no error messages in console but nothing happen.

[toc] | [prev] | [next] | [standalone]


#32684

Fromjonas.thornvall@gmail.com
Date2017-03-04 14:28 -0800
Message-ID<3293ad20-bb89-4ca8-92a8-3764deb980fc@googlegroups.com>
In reply to#32683
Den lördag 4 mars 2017 kl. 23:02:38 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 4 mars 2017 kl. 22:55:28 UTC+1 skrev jonas.t...@gmail.com:
> > This is the attributes of my array element 
> >      arr[m] = {
> >             nodelinks: [],
> >             hold: false,
> >             rposX: 0,
> >             rposY: 0,
> >             rwidth: 10,
> >             rheigth: 10,
> >             rcolor: "red",
> >             bcolor: "green",
> >             print: true
> >         };
> > 
> > When i remove an element i want to replace it with the last one, and then pop last element.
> > 
> > How do i do that? Well i know howto do it doing each attribute at a time, but is there a way to just update the full array element in one step? 
> > 
> > function reOrderArr(){
> >   arr[t]=arr[arr.length-1];
> >   arr.pop();
> > }
> 
> In the case above arr.pop(); will not remove any element, nor will it do the copy. Array size stay the same i get no error messages in console but nothing happen.

Solved

[toc] | [prev] | [next] | [standalone]


#32688

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-03-04 15:35 -0800
Message-ID<d6f1d860-e104-494a-acf1-28c8fa45cb2b@googlegroups.com>
In reply to#32684
On Saturday, March 4, 2017 at 4:28:20 PM UTC-6, jonas.t...@gmail.com wrote:
> Den lördag 4 mars 2017 kl. 23:02:38 UTC+1 skrev jonas.t...@gmail.com:
> > Den lördag 4 mars 2017 kl. 22:55:28 UTC+1 skrev jonas.t...@gmail.com:

> Solved

As the saying goes: "Measure twice and cut once" [1]

To repeat and paraphrase myself and others in this case: Think for a few minutes before posting follow-ups.

The amount of back-to-back posts makes few people want to even bother reading any in detail. I myself admit to barely spot-checking at this point.

[1] <https://en.wiktionary.org/wiki/measure_twice_and_cut_once>

[toc] | [prev] | [next] | [standalone]


#32685

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-03-04 23:53 +0100
Message-ID<XnsA72EF313A3B2Feejj99@194.109.6.166>
In reply to#32683
jonas.thornvall@gmail.com wrote on 04 Mar 2017 in comp.lang.javascript:

>> function reOrderArr(){

function reOrderArr(t){

>>   arr[t]=arr[arr.length-1];
>>   arr.pop();
>> }
> 
> In the case above arr.pop(); will not remove any element, nor will it do
> the copy. Array size stay the same i get no error messages in console
> but nothing happen. 

Works fine here:

<script type='text/javascript'> 'use strict'

let arr = ['a','b','c'];
document.write(arr + '<br>'); // a,b,c

reOrderArr(0);
document.write(arr + '<br>'); // c,b

function reOrderArr(t){
   arr[t] = arr[arr.length-1];
   arr.pop();
};

</script>




-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#32686

Fromjonas.thornvall@gmail.com
Date2017-03-04 15:11 -0800
Message-ID<e454fb0e-fed3-40c8-a963-4aa1d2a7e7db@googlegroups.com>
In reply to#32685
Den lördag 4 mars 2017 kl. 23:53:44 UTC+1 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 04 Mar 2017 in comp.lang.javascript:
> 
> >> function reOrderArr(){
> 
> function reOrderArr(t){
> 
> >>   arr[t]=arr[arr.length-1];
> >>   arr.pop();
> >> }
> > 
> > In the case above arr.pop(); will not remove any element, nor will it do
> > the copy. Array size stay the same i get no error messages in console
> > but nothing happen. 
> 
> Works fine here:
> 
> <script type='text/javascript'> 'use strict'
> 
> let arr = ['a','b','c'];
> document.write(arr + '<br>'); // a,b,c
> 
> reOrderArr(0);
> document.write(arr + '<br>'); // c,b
> 
> function reOrderArr(t){
>    arr[t] = arr[arr.length-1];
>    arr.pop();
> };
> 
> </script>
> 
> 
> 
> 
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Well i tried it but it doesn't work i had to do like this.


function reOrderArr(t) {
    arr[t].nodelinks = arr[arr.length - 1].nodelinks;
    arr[t].hold = arr[arr.length - 1].hold;
    arr[t].rposX = arr[arr.length - 1].rposX;
    arr[t].rposY = arr[arr.length - 1].rposY;
    arr[t].rwidth = arr[arr.length - 1].rwidth;
    arr[t].rheigth = arr[arr.length - 1].rheigth;
    arr[t].rcolor = arr[arr.length - 1].rcolor;
    arr[t].bcolor = arr[arr.length - 1].bcolor;
    arr[t].print = arr[arr.length - 1].print;
    last=arr.length-1;
    arr.pop();
    for (var s=0;s<arr.length;s++){
      for (var v=0;v<arr[s].nodelinks.length;v++){
        if (arr[s].nodelinks[v]==last) arr[s].nodelinks[v]=t;
      }
    }
}

[toc] | [prev] | [next] | [standalone]


#32692

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-03-05 10:40 +0100
Message-ID<XnsA72F6C902E48Aeejj99@194.109.6.166>
In reply to#32686
jonas.thornvall@gmail.com wrote on 05 Mar 2017 in comp.lang.javascript:

>> Works fine here:
>> 
>> <script type='text/javascript'> 'use strict'
>> 
>> let arr = ['a','b','c'];
>> document.write(arr + '<br>'); // a,b,c
>> 
>> reOrderArr(0);
>> document.write(arr + '<br>'); // c,b
>> 
>> function reOrderArr(t){
>>    arr[t] = arr[arr.length-1];
>>    arr.pop();
>> };
>> 
>> </script>

> Well i tried it but it doesn't work

This is just nonsense, my example works fine.

> i had to do like this.

That it does not work like you expect it to work,
means you should change your expectations, not the code.


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web