给定第二个数组,如何找到某个值的位置?

时间:2016-04-18 21:37:52

标签: ios arrays swift

我正在比较两个数组,我需要弄清楚重复发生在哪个位置。我必须映射数组,但不确定如何做到这一点。最后我想要一个过滤后的数组。 1表示存在重复,""表示没有重复。

var Strarr = ["1", "2", "3", "4"]
var importarr = ["1", "3"]

filteredArray = ["1", "", "1", ""]

3 个答案:

答案 0 :(得分:2)

//...

foreach(var field in newFields)
{
    // ...

    PropertyInfo propInfo = typeof(CustomObject).GetProperty(field.Key);
    var value = propInfo.GetValue(customObject, null);

    PropertyInfo propInfo = null;

    // handles TEnum
    if (propInfo.PropertyType.IsEnum)
    {
        propInfo.SetValue(customObject, Enum.ToObject(propInfo.PropertyType, field.Value), null);
    }
    // handles TEnum?
    else if (Nullable.GetUnderlyingType(propInfo.PropertyType)?.IsEnum == true)
    // if previous line dont compile, use the next 2
    //else if (Nullable.GetUnderlyingType(propInfo.PropertyType) != null &&
    //         Nullable.GetUnderlyingType(propInfo.PropertyType).IsEnum)
    {
        propInfo.SetValue(customObject, Enum.ToObject(Nullable.GetUnderlyingType(propInfo.PropertyType), field.Value), null);
    }
    else
    {
        propInfo.SetValue(customObject, field.Value, null);
    }

}

有点短。

答案 1 :(得分:0)

这将有效:

var Strarr = ["1", "2", "3", "4"]
var importarr = ["1", "3"]

var newArray = Strarr
for i in 0..<Strarr.count {
    if importarr.contains(Strarr[i]) {
        newArray[i] = "1"
    }
    else{
        newArray[i] = "0"
    }
}
print(newArray)

打印"["1", "0", "1", "0"]\n"

答案 2 :(得分:0)

这应该做到

var Strarr = ["1", "2", "3", "4"]
var importarr = ["1", "3"]
var filteredArray = Strarr.map(function(val){
   return importarr.indexOf(val) != -1 ? "1" : "";
});