简体   繁体   English

使用javascript从数组中删除特定值

[英]remove particular value from array using javascript

在此处输入图片说明

I have these type of array in javascript.Now i want to remove the value from array which name is "PropertyType[]".How to do it.Here i upload the picture of array.Here in Newremoveurl array i get the array value.我在javascript中有这些类型的数组。现在我想从数组中删除名称为“PropertyType[]”的值。如何做。在这里我上传数组的图片。在Newremoveurl数组中我得到数组值。

var Newremoveurl = [];
        var Parant_name = 'PropertyType';
        $.each(Removeurl_array, function( key, value){
            var decoded_key = decodeURI(value);
            if ($.inArray(Parant_name, Newremoveurl)!='-1') {

            } 
            Newremoveurl[key]=decoded_key;
        }); 

you can filter your array to remove these with standard javascript for an exact match - you can remove like this您可以过滤您的数组以使用标准 javascript 删除它们以获得精确匹配 - 您可以像这样删除

const filteredResults = myArray.filter(item => item === "PropertyType[]")

or for values which contain a string或对于包含字符串的值

const filteredResults = myArray.filter(item => item.indexOf("PropertyType[]") ===-1)
for (var i=myArr.length; i--;) {
     if (myArr[i].indexOf("PropertyType")>=0) break;
}

Value of i will be -1 if PropertyType* does not exist, or else it would be the index of the element.如果 PropertyType* 不存在, i 的值为 -1,否则它将是元素的索引。

Then cut the element from the array with然后从数组中剪切元素

if (index > -1) {
    myArr.splice(index, 1);
}

You can run this solution till the value of i becomes -1, then your array would hopefully be free of the element you do not require.您可以运行此解决方案,直到 i 的值变为 -1,然后您的数组有望没有您不需要的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM