Remove column name from array in JSON

I am getting format like this in an array here is my array =

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

      

I need a format like this

var array= [

    "Jaipur",
    "Mumbai"
];

      

which should be done in JSON so that I can get the array I want.

+3


source to share


5 answers


You can use Set to get unique values, and spread to the array again.



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

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

console.log(res)
      

Run codeHide result


+7


source




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>
      

Run codeHide result


+2


source


You can reduce

array:

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)
      

Run codeHide result


+1


source


 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);

}

      

-1


source


try it

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);
}

      

-2


source







All Articles