如何删除嵌套数组对象?

时间:2016-12-10 09:32:11

标签: javascript c# asp.net arrays asp.net-mvc

how to Delete red Mark item

我添加了我的JavaScript代码 ...我没有生成嵌套数组对象删除函数任何人都可以帮助我

如何删除nestobj:

"价值":" 10"

"价值":" 20"

"价值":" 20"

<script>
$(function () {
    var tr;
    var obj = [{ obj1: { "name": "v1", nestobj: [{ "value": "10" }, { "value": "20" }, { "value": "20" }] } }];

    obj.push({ obj1: { "name": "v2", nestobj: [{ "value": "abc" }, {"value": "xyz"}] } });


    for (var i = 0; i < obj.length; i++)
    {
        tr = $('<tr/>');
        tr.append("<td>" + obj[i].obj1.name + "</td>")
        $('tbody').append(tr);


        for (var j = 0; j < obj[i].obj1.nestobj.length; j++)
        {                
            tr.append("<p>" + obj[i].obj1.nestobj[j].value + " <input type='submit' value='Delete' onclick='&quot;);'> </p>")
            $('tbody').append(tr);

        }       
        tr.append("<td>" + obj[i].obj1.nestobj.length + " <input type='submit' value='Remove All' onclick='&quot;);'> </td>")
        $('tbody').append(tr);

    }
});

1 个答案:

答案 0 :(得分:0)

更改您的功能如下:

$(function () {
    var tr;
    var obj = [{ obj1: { "name": "v1", nestobj: [{ "value": "10" }, { "value": "20" }, { "value": "20" }] } }];

    obj.push({ obj1: { "name": "v2", nestobj: [{ "value": "abc" }, {"value": "xyz"}] } });


    for (var i = 0; i < obj.length; i++)
    {
        tr = $('<tr/>');
        tr.append("<td>" + obj[i].obj1.name + "</td>")
        $('tbody').append(tr);


        for (var j = 0; j < obj[i].obj1.nestobj.length; j++)
        {                
            var $delBtn = $("<input>",{type:"submit",value:"Delete"});
            var $p = $("<p>",{text:obj[i].obj1.nestobj[j].value});
            $delBtn.on("click",function(){
                $(this).parent().remove();
            });
            $p.append($delBtn);
            tr.append($p);
            $('tbody').append(tr);

        }     
        var $removeAll = $("<input>",{type:"submit",value:"Remove All"});
        var $col = $("<td>",{text: obj[i].obj1.nestobj.length});  
        $removeAll.on("click",function(){
            $(this).closest("tr").find("p").remove();
        });
        $col.append($removeAll);
        tr.append($col);
        $('tbody').append(tr);

    }
});

});

所有关于向您的输入添加事件以删除项目

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
        <tbody></tbody>
    </table>
&#13;
#include <stdio.h>
#include <stdlib.h>
//-----------------------------------
char* getstring(void)
  {
  char *x;
  int i = 0;
  x = malloc(sizeof(char));
  int ch;
  while ( ((ch = getchar()) != EOF) && (i<99))
  {
     x[i] = (char)ch;
     i++;
     x = realloc(x ,sizeof(char)*(i+1));
  }
  x[i]=0;
  return x;
  } 
//-----------------------------------
int main()
  {
  char *s;
  s=getstring();
  printf("\nYou entered : %s\n",s);
  free(s);
  return 0;
  }
//-----------------------------------

/* 

On Ubuntu linux you have to press ENTER and ctrl-d at the end of your keyboard input

output:

user@ubuntu1:~/Desktop/getstr$ ./tst
This is a test...

You entered : This is a test...



*/
&#13;
&#13;
&#13;