我如何替换另一个值变化的阵列? [英] How do I replace a changing array with another value?

查看:85
本文介绍了我如何替换另一个值变化的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个约会数组,它包含当前日加10天,我从导入数据库的数组。我也有一个动物名数组。基于什么是进口的我要放置动物名在筛选阵列中的过滤阵列。例如:

 日期数组:`[9,10,11,12,13,14,15,16,17,18]`
  进口日期数组从DB:[9,12,14,18]
  从数据库进口的动物名称数组:狗,猫,虎,羊]

这就是我想要过滤的动物名看起来像

 过滤动物名数组:[狗,,猫,虎,,,羊]

我知道我所提供的code是错的,我觉得我incorretly接近这一。我应该怎么做呢?

 为(VAR J = 0; J< self.arrayofweek.count; J ++){
                        对于(VAR T = 0; T< self.datesfromdb.count;吨++){
                            如果(self.date [T] == self.datearray [J]){
                                self.namefiltered [T] = self.tutorname [J]。
                                 打印(过滤名\\(self.namefiltered))
                            }
                        }
                    }


解决方案

数据:

 让日期= [9,10,11,12,13,14,15,16,17,18]
让进口= [9,12,14,18]
让动物= [狗,猫,虎,羊]

重做:

 让过滤= {dates.map拉链(进口,动物).MAP {$ 0.0次} .indexOf($ 0){.MAP动物[$ 0]}? }

输出:

 打印(阵列)// [狗,,,猫,,虎,,,,羊]

基于FranMowinckel的回答

,但100%的安全。

I have a date array which contains the current day plus 10 days, and I am importing an array from a database. I also have an animal name array. Based on what is imported I want to place the animal name in a filtered array in a filtered array. For example:

  date array: `[9, 10, 11, 12, 13, 14, 15, 16, 17, 18]`
  imported date array from db: [9, 12, 14, 18]
  imported animal name array from db: [dog, cat, tiger, sheep]

This is what I want the filtered animal name to look like

filtered animal name array: [dog, "", "", cat, "", tiger, "", "", "", sheep]

I know the code I provided is wrong, I feel I am approaching this incorretly. How should I do this?

 for(var j = 0; j < self.arrayofweek.count; j++){
                        for(var t = 0; t < self.datesfromdb.count; t++){
                            if(self.date[t] == self.datearray[j]){
                                self.namefiltered[t] = self.tutorname[j]
                                 print("filtered name \(self.namefiltered)")
                            }
                        }
                    }

解决方案

Data:

let dates = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
let imported = [9, 12, 14, 18]
let animals = ["dog", "cat", "tiger", "sheep"]

Reworked:

let filtered = dates.map { zip(imported, animals).map { $0.0 }.indexOf($0).map { animals[$0] } ?? "" }

Output:

print(array) // ["dog", "", "", "cat", "", "tiger", "", "", "", "sheep"]

Based on FranMowinckel's answer, but 100% safe.

这篇关于我如何替换另一个值变化的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆