从JSON中的数组中删除列名

时间:2017-05-22 10:23:47

标签: javascript jquery arrays json

我在数组中得到这样的格式 这是我的数组=

sc create "ServiceName" binPath= "G:\services\service.exe" DisplayName= "ServiceName" start= "auto" obj= "username"   password= "password"

我想要这样的格式

  var array= [{"address":"Jaipur"},{"address":"Mumbai"},{"address":"Mumbai"}]

在JSON中应该做些什么,这样我才能获得所需的数组。

5 个答案:

答案 0 :(得分:7)

您可以使用Set获取唯一值,并将spread再次添加到数组中。

var array= [{"address":"Jaipur"},{"address":"Mumbai"},{"address":"Mumbai"}];

var res = [...new Set(array.map(x => x.address))];

console.log(res)

答案 1 :(得分:2)



var array= [{"address":"Jaipur"},{"address":"Mumbai"},{"address":"Mumbai"}];
var address=[];

$.each(array,function(add,val){
address.push(val.address);
});
var address = Array.from(new Set(address));
console.log(address);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

你可以reduce数组:

&#13;
&#13;
var array= [{"address":"Jaipur"},{"address":"Mumbai"},{"address":"Mumbai"}];

var formatedArray = array.reduce((output, item) => {
  if(!output.includes(item.address)) {
    output.push(item.address)
  }
  return output
}, [])

console.log(formatedArray)
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

 string json = DataTableToJSON(dt_main); 
 jsonnew = @"{""Data"":" + json + "}";



private static string DataTableToJSON(DataTable table)
{

    List<string[]> result = table.Rows
 .Cast<DataRow>()
 .Select(row => row.ItemArray
     .Select(x => x.ToString())
     .ToArray())
 .ToList();

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Serialize(result);

    //------another method----------------
    //ArrayList arr = new ArrayList(); 
    //List<ArrayList> list = new List<ArrayList>();

    //foreach (DataRow row in table.Rows)
    //{ 
    //    arr = new ArrayList();
    //    foreach (DataColumn col in table.Columns)
    //    { 
    //       arr.Insert(col.Ordinal, row[col]);
    //    }

    //     list.Add(arr); 
    //} 

   // JavaScriptSerializer serializer = new JavaScriptSerializer();
   // return serializer.Serialize(list);

}

答案 4 :(得分:-2)

试试这个

var array= [{"address":"Jaipur"},{"address":"Mumbai"},{"address":"Mumbai"}];
var array_new=[];
for(var i=0;i<array.length;i++){
    array_new.push(array[i].address);
}