有两个数组,例如:
class TeamDetailsController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
print(textView.text)
}
lazy var scrollView: UIScrollView = {
let sv = UIScrollView(frame: self.view.bounds)
sv.backgroundColor = .white
sv.translatesAutoresizingMaskIntoConstraints = false
sv.layer.borderWidth = 5
return sv
}()
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "team-tony")
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 50
imageView.layer.masksToBounds = true
imageView.clipsToBounds = true
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Jared 'Donald' Dunn"
label.font = UIFont.systemFont(ofSize: 20, weight: .bold)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let textView: UITextView = {
var tv = UITextView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.textColor = .black
tv.font = UIFont.systemFont(ofSize: 16)
tv.isEditable = false
return tv
}()
func setupViews() {
view.backgroundColor = .white
view.addSubview(scrollView)
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
scrollView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
scrollView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true
scrollView.addSubview(profileImageView)
profileImageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
profileImageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 20).isActive = true
scrollView.addSubview(nameLabel)
nameLabel.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
nameLabel.topAnchor.constraint(equalTo: profileImageView.bottomAnchor, constant: 10).isActive = true
scrollView.addSubview(textView)
textView.topAnchor.constraint(equalTo: nameLabel.bottomAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
textView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 20).isActive = true
textView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 20).isActive = true
}
}
在此操作之后,我想将第二个元素添加到第一个元素中,arr1 = ["a", "b"];
arr2 = ["c", "d"];
应该看起来像arr1
。没关系,["a", "b", "c", "d"]
会发生什么。
我尝试了经典方法:arr2
,结果看起来像:arr1.push(arr2)
。
答案 0 :(得分:2)
var arr1 = ["a", "b"];
var arr2 = ["c", "d"];
arr1 = arr1.concat(arr2);
console.log(arr1)
答案 1 :(得分:2)
您可以使用ES6语法来做到这一点:
您可以进行以下操作:
const arr1 = ["a", "b"];
const arr2 = ["c", "d"];
arr1 = [...arr1,...arr2]
console.log(arr1)
有关点差运算符的定义:
允许将迭代器(例如数组表达式或字符串)扩展到期望零个或多个参数(用于函数调用)或元素(用于数组文字)的位置,或者将对象表达式扩展到零或期望有更多的键值对(用于对象文字)。 (定义来自MDN)
在ES5语法中,您应该使用.concat()
函数,但现在在ES6中更容易
答案 2 :(得分:0)
使用传播语法:
var arr1 = ["a", "b"];
var arr2 = ["c", "d"];
arr1 = [...arr1,...arr2];
console.log(arr1);
使用 Array.concat():
var arr1 = ["a", "b"];
var arr2 = ["c", "d"];
arr1 = arr1.concat(arr2);
console.log(arr1);