gpt4 book ai didi

ios - 使用 Array.filter 而不是对象获取索引列表

转载 作者:可可西里 更新时间:2023-11-01 00:33:21 24 4
gpt4 key购买 nike

有没有办法获取过滤索引列表而不是对象。

class Object
{
var name
var goal
}

var array<Object> = Array<Object>()

var filteredIndexes = array.filter{$0.name = "Sane"} // How to implement this?

最佳答案

有多种方法可以实现您的目标。例如,您可以过滤 Array.indices 而不是数组本身。

独立示例:

struct Object {
let name:String
let goal:String
}

let objects = [Object(name: "John", goal: "a"),Object(name: "Jane", goal: "a"),Object(name: "John", goal: "c"),Object(name: "Pete", goal: "d")]
let nameToBeFound = "John"
let filteredIndices = objects.indices.filter({objects[$0].name == nameToBeFound}) //[0,2]

关于ios - 使用 Array.filter 而不是对象获取索引列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49171482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com