मैं अपनी तालिका/सरणी से एक वस्तु (सूचकांक) को हटाना चाहता हूं

var item = [
{name:'test1',variable:20},
{name:'test2',variable:20},
{name:'test3',variable:20},
{name:'test4',variable:20},
{name:'test5',variable:20},
{name:'test6',variable:20},
{name:'test7',variable:20},
{name:'test8',variable:20}]

उदाहरण के लिए मैं ऑब्जेक्ट नंबर 2 (item.name=test3) को हटाना चाहता हूं, मैं इसे कैसे कर सकता हूं?

-3
iCrmx 3 अप्रैल 2018, 18:39

4 जवाब

सबसे बढ़िया उत्तर

आप splice() और findIndex() विधियों का उपयोग कर सकते हैं।

var item = [{name:'test1',variable:20},{name:'test2',variable:20},{name:'test3',variable:20},{name:'test4',variable:20},{name:'test5',variable:20},{name:'test6',variable:20},{name:'test7',variable:20},{name:'test8',variable:20}]

item.splice(item.findIndex(({name}) => name == 'test3'), 1);
console.log(item)
0
Nenad Vracar 3 अप्रैल 2018, 15:42
function remove(array, element) {
    return array.filter(e => e !== element);
}

const vowelsAndX = ["a", "e", "i", "o", "u", "x"];
const vowels = remove(vowelsAndX, "x");
vowels.toString(); // "a,e,i,o,u"

----------------------------------------------------------------------------


var item = [
    {name:'test1',variable:20},
    {name:'test2',variable:20},
    {name:'test3',variable:20},
    {name:'test4',variable:20},
    {name:'test5',variable:20},
    {name:'test6',variable:20},
    {name:'test7',variable:20},
    {name:'test8',variable:20}];

    function remove(array, element) {
        return array.filter(e => e !== element);
    }

    const restOfItems = remove(item, item[0]);
    console.log(restOfItems)
0
Bruno Garcia 5 अप्रैल 2018, 07:33
var item = [
{name:'test1',variable:20},
{name:'test2',variable:20},
{name:'test3',variable:20},
{name:'test4',variable:20},
{name:'test5',variable:20},
{name:'test6',variable:20},
{name:'test7',variable:20},
{name:'test8',variable:20}]

//removing 1 element from number 2
var removeObj = item.splice(2,1);

console.log(item);
0
Kenny Heenatigala 3 अप्रैल 2018, 15:54

आप ब्याह का भी उपयोग कर सकते हैं, लेकिन सावधान रहें क्योंकि यह वास्तविक सरणी को संशोधित करता है

var item = [
{name:'test1',variable:20},
{name:'test2',variable:20},
{name:'test3',variable:20},
{name:'test4',variable:20},
{name:'test5',variable:20},
{name:'test6',variable:20},
{name:'test7',variable:20},
{name:'test8',variable:20}]

item.splice(2,1)

console.log(item)
0
Abslen Char 3 अप्रैल 2018, 15:51