JavaScript deletes elements in an array/object

Source: Internet
Author: User
One, array:
  1. Code:
    /*@desc:删除数组中的元素@param arr 原数组@param item 数组索引,只能是数字索引@param len 要删除的长度@return arr 删除元素后的数组*/function unset(arr,item,len = 1){arr.splice(item,len)return arr}
  2. Test:
    var arr = new Array(1,2,3,4)var ret = unset(arr,1,2)console.log(ret)
  3. Output:
    [ 1, 4 ]
    Two, JSON object:
  4. Code:
    /*@desc:删除对象中的元素@param arr 原对象@param item 对象索引@return arr 删除元素后的对象*/function unsetobj(obj,item){delete obj[item]return obj}
  5. Test:
    var obj = {name:‘lee‘,sex:‘male‘}var ret = unsetobj(obj,‘sex‘)console.log(ret)
  6. Output:
    { name: ‘lee‘ }

JavaScript deletes elements in an array/object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.