gpt4 book ai didi

javascript - 如何在 angularJS 或 javascript 中将两个不同的数组值转换为字符串

转载 作者:行者123 更新时间:2023-11-28 12:19:31 27 4
gpt4 key购买 nike

给定这两个数组:

$scope.city= [{id :'NewYork' } , {id : 'Chicago'}];
$scope.color = [{id : 'blue' } , {id : 'Green'}];

我只需要两个数组的值,如下所示:

$scope.string = 'NewYork_Chicago_blue_Green';

最佳答案

使用Array#map将每个数组转换为字符串数组,然后使用Array#concat它们,并使用下划线Array#join :

var city = [ {id :'NewYork' } , {id : 'Chicago'}];
var color = [{id : 'blue' } , {id : 'Green'}];

function getId(o) {
return o.id;
}

var result = city.map(getId).concat(color.map(getId)).join('_');

console.log(result);

关于javascript - 如何在 angularJS 或 javascript 中将两个不同的数组值转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41596776/

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