将对象从一个数组移动到另一个数组

时间:2017-08-28 13:07:22

标签: javascript angular typescript fuse

我有一个对象,其中一个属性是一个对象数组,想法是如果一个条件为真,则将对象从该数组移动到没有新对象。

public $onInit(): void {
  this.getTicket();
}

public ticket: any; // Object with the array
public comments: any = []; // New array to move the elements
public getTicket(): void {
    this.ticketService
        .getTicketComplete(this.$stateParams.ticketID)
        .then((response: any) => {
            this.ticket = response;
            this.stringToDate(this.ticket);
            this.ticket.messages.forEach((elem, index) => {
                if (elem.type === "comment") {
                    this.ticket.messages.splice(index, 1);
                    this.comments.push(elem);
                }
            });
            console.log(this.ticket);
    });
}

我遇到的问题是下一个问题: 数组有对象类型,消息和注释,如果数组有2条消息和3条注释,它应该推送到新数组3条评论并留下2条消息,但只移动2条评论。

任何想法。谢谢你的帮助。

3 个答案:

答案 0 :(得分:5)

这是的方式:

import turtle

GRID_UNIT = 50
GRID_SIZE = 5  # code below assumes this is an odd number

####################################################################
def Combat():
print('You encounter a raverous Spider! do you wish to fight it?')
Fight = str(input())
if Fight == 'Yes':
    #########Battle Code Warrior############
    import random
    import time


    While_Loop = 1
    ###########Battle 1############
    print('============================================')
    while While_Loop == 1:
            if Foe >= 0:
                time.sleep(1)
                print('You did', Player_Dmg,'Dmg')
                Foe = Foe - Player_Dmg # checks health remaining
                print('Your Foe has ',Foe,'Hp left')
                print('=============================')
                time.sleep(0.5)
                Player_Dmg = random.randrange(0,10)
                if Foe <= 0:
                    print('You killed it')
                    break

                else:
                    time.sleep(1)
                    if life >= 0:
                        life = life - Foe_Dmg
                        print('You took ', Foe_Dmg,'Dmg')
                        print('You Have', life, 'Hp left')
                        print('=============================')
                        time.sleep(0.5)
                        Foe_Dmg = random.randrange(0, 10)
                        if life <= 0:
                            print('You are dead')

            else:
                print('You die')
                life = 0
                break

elif Fight == 'No':
    print('You Sprint Frantically away from the spider while it chases  you down!')    
def Map_Starting_Area():

turtle.pu()
turtle.goto(-GRID_SIZE/2 * GRID_UNIT, -GRID_SIZE/2 * GRID_UNIT)
turtle.ht()
turtle.pd()

####### Grid ###########

for _ in range(GRID_SIZE // 2):
    turtle.forward(GRID_SIZE * GRID_UNIT)
    turtle.left(90)
    turtle.forward(GRID_UNIT)
    turtle.left(90)
    turtle.forward(GRID_SIZE * GRID_UNIT)
    turtle.right(90)
    turtle.forward(GRID_UNIT)
    turtle.right(90)

turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)

for _ in range(GRID_SIZE // 2):
    turtle.forward(GRID_SIZE * GRID_UNIT)
    turtle.left(90)
    turtle.forward(GRID_UNIT)
    turtle.left(90)
    turtle.forward(GRID_SIZE * GRID_UNIT)
    turtle.right(90)
    turtle.forward(GRID_UNIT)
    turtle.right(90)

turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.pu()
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(25)
###############################################################
# Square = Town
# Circle = Fight
# Arrow = Quest Marker
# Turtle = Weapon Shops
# Triangle = Potion Shop
# Hollow Circle = Chest
turtle.shape('square')
turtle.stamp()
turtle.fd(50)
turtle.right(90)
turtle.fd(50)
turtle.shape('circle')
turtle.stamp()
turtle.back(50)
turtle.left(90)
turtle.fd(50)
turtle.left(90)
turtle.fd(50)
turtle.shape('circle')
turtle.stamp()
turtle.back(50)
turtle.right(90)
turtle.fd(50)
turtle.right(90)
turtle.fd(50)
turtle.shape('circle')
turtle.stamp()
turtle.fd(100)
turtle.shape('square')
turtle.stamp()
turtle.rt(90)
turtle.fd(140)
turtle.rt(90)
turtle.pd()
turtle.circle(10)
turtle.pu()
turtle.fd(150)
turtle.right(90)
turtle.back(10)



###############################################################    
turtle.st()
###############################################################

turtle.speed('fastest')

Map_Starting_Area()

turtle.color('orange')
turtle.shape('classic')

 print('You Continue with your Journey')
 print('You leave the protection of your farm and head into the open land')
print('You Come to a cross road')

while True:
    direction = input('Would you like to go North, South, East or West: ').lower()

if direction == 'north' and turtle.ycor() < GRID_UNIT * (GRID_SIZE//2 - 0.5):
    turtle.sety(turtle.ycor() + GRID_UNIT)
    turtle.update()
elif direction == 'south' and turtle.ycor() > -GRID_UNIT * (GRID_SIZE//2 - 0.5):
    turtle.sety(turtle.ycor() - GRID_UNIT)
    turtle.update()
elif direction == 'east' and turtle.xcor() < GRID_UNIT * (GRID_SIZE//2 - 0.5):
    turtle.setx(turtle.xcor() + GRID_UNIT)
    turtle.update()
elif direction == 'west' and turtle.xcor() > -GRID_UNIT * (GRID_SIZE//2 - 0.5):
    turtle.setx(turtle.xcor() - GRID_UNIT)
    turtle.update()
elif direction == 'quit':
    break
if turtle.pos == (0.00,50.00):
    combat()

这是如何做到的一个例子:

var array1 = [1, 2, 3, 4, 5];
var array2 = [];

array1.forEach(function(elem, index) {
  array1.splice(index, 1);
  array2.push(elem);
});

console.log(array1); //[2, 4]
console.log(array2); //[1, 3, 5]

特定用例:

var array1 = [1, 2, 3, 4, 5];
var array2 = [];

for(var i = 0; i < array1.length; i++) {
  array2.push(array1[i]);
  array1.splice(i, 1);
  i--; //decrement i IF we remove an item
}

console.log(array1); //[]
console.log(array2); //[1, 2, 3, 4, 5]

答案 1 :(得分:0)

当你循环遍历数组时,你正在删除元素 - 这绝不是一个好主意。解决此问题的更好方法是首先将它们添加到this.comments,并在foreach完成后,开始循环遍历this.com并从消息中删除此数组中的那些。

答案 2 :(得分:0)

我们在这里走宝贝的异步方式:

var array1 = [1, 2, 3, 4, 5];
var array2 = [];

async function fillArray() {
  array1.forEach(function(elem, index) {
    console.log("elem: ", elem)
    array2.push(elem); 
  })
}

async function emptyArray(){
  fillArray().then(() =>{
    array1.length = 0; 
 })
}

emptyArray().then(() => { 
  console.log("array1: ", array1); //[]
  console.log("array2: ", array2); //[1, 2, 3, 4, 5]
})

另一种有条件的大举欢呼:

var array1 = [1, 2, 3, 4, 5];
var array1Length= array1.length;
var array2 = [];
 
  array1.forEach((elem, index) => {
    array2.push(elem); 
    if(array2.length === array1Length) array1.length=0
  })
  
  console.log("array1: ", array1); //[]
  console.log("array2: ", array2); //[1, 2, 3, 4, 5]