我们如何基于Angular 6中的另一个数组从数组中删除对象

时间:2019-02-28 06:38:11

标签: angular typescript angular6

我有两个这样的数组:

var skillArray = [
{ID: 1, name: "الغطس"},
{ID: 2, name: "الحريق"},
{ID: 3, name: "المعالجة"},
{ID: 4, name: "الميكانيكا"},
{ID: 5, name: "السيول"},
{ID: 6, name: "التغريز"},
{ID: 7, name: "الكهرباء"},
{ID: 8, name: "الاحتفالات"},
{ID: 9, name: "المناسبات"},
{ID: 10, name: "الصلاة"}
]

var splitstr = ["9", "7"]

如您所见,ID 9和7匹配,我希望能够将它们从skillArray中删除。我怎么写这个?

5 个答案:

答案 0 :(得分:4)

skillArray.filter()的每个元素使用splitstr

由于splitstr的元素为字符串,因此必须转换类型以进行比较。

var skillArray = [
{ID: 1, name: "الغطس"},
{ID: 2, name: "الحريق"},
{ID: 3, name: "المعالجة"},
{ID: 4, name: "الميكانيكا"},
{ID: 5, name: "السيول"},
{ID: 6, name: "التغريز"},
{ID: 7, name: "الكهرباء"},
{ID: 8, name: "الاحتفالات"},
{ID: 9, name: "المناسبات"},
{ID: 10, name: "الصلاة"}
]

var splitstr = ["9", "7"]

var arr = skillArray.filter(e => !splitstr.includes('' + e.ID))

console.log(arr)

答案 1 :(得分:3)

这是一个有效的示例:

var skillArray = [
    {ID: 1, name: "الغطس"},
    {ID: 2, name: "الحريق"},
    {ID: 3, name: "المعالجة"},
    {ID: 4, name: "الميكانيكا"},
    {ID: 5, name: "السيول"},
    {ID: 6, name: "التغريز"},
    {ID: 7, name: "الكهرباء"},
    {ID: 8, name: "الاحتفالات"},
    {ID: 9, name: "المناسبات"},
    {ID: 10, name: "الصلاة"}
]

var splitstr = ["9", "7"]

for (var i = 0; i < splitstr.length; i++) {
    for (var j = 0; j < skillArray.length; j++) {
        if (splitstr[i] == skillArray[j]['ID']) {
            skillArray.splice(j, 1);
        }
    }
}

console.log(skillArray);

splice方法可用于在数组中添加或删除元素。第一个参数指定开始添加或删除元素的位置。第二个参数指定要删除的元素数。

答案 2 :(得分:3)

如果要修改相同的数组,请使用splice

然后用findIndex获取要删除的匹配项的索引。

var skillArray = [
{ID: 1, name: "الغطس"},
{ID: 2, name: "الحريق"},
{ID: 3, name: "المعالجة"},
{ID: 4, name: "الميكانيكا"},
{ID: 5, name: "السيول"},
{ID: 6, name: "التغريز"},
{ID: 7, name: "الكهرباء"},
{ID: 8, name: "الاحتفالات"},
{ID: 9, name: "المناسبات"},
{ID: 10, name: "الصلاة"}
]

var splitstr = ["9", "7"]

splitstr.forEach(item => {
  const i = skillArray.findIndex(skill => skill.ID == item);
  if(i > -1) {
       skillArray.splice(i, 1)
  }
})

console.log(skillArray);

答案 3 :(得分:3)

volatile

答案 4 :(得分:2)

只需使用@Configuration @Order(1) public static class BasicAuthenticationAdapter extends WebSecurityConfigurerAdapter { private final AuthenticationEntryPoint authEntryPoint; @Autowired public BasicAuthenticationAdapter(AuthenticationEntryPoint authEntryPoint) { this.authEntryPoint = authEntryPoint; } protected void configure(HttpSecurity http) throws Exception { http .antMatcher("/orders**") .authorizeRequests() .anyRequest().hasRole("USER") .and() .csrf().disable() .httpBasic().authenticationEntryPoint(authEntryPoint) .and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); } } 即可实现,方法如下-

includes

注意: var skillArray = [ {ID: 1, name: "الغطس"}, {ID: 2, name: "الحريق"}, {ID: 3, name: "المعالجة"}, {ID: 4, name: "الميكانيكا"}, {ID: 5, name: "السيول"}, {ID: 6, name: "التغريز"}, {ID: 7, name: "الكهرباء"}, {ID: 8, name: "الاحتفالات"}, {ID: 9, name: "المناسبات"}, {ID: 10, name: "الصلاة"} ] var splitstr = ["9", "7"]; skillArray.forEach((obj, index) => { if(splitstr.includes(obj['ID'].toString())) { skillArray.splice(index, 1); } }); console.log(skillArray); 是将数字与字符串进行比较所必需的。