3

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

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

I want format like this

var array= [

    "Jaipur",
    "Mumbai"
];

what things should be done to in JSON so i can get desired array.

5 Answers 5

7

You can use Set to get unique values, and spread it into an array again.

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

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

console.log(res)

Sign up to request clarification or add additional context in comments.

4 Comments

Amazing.got new knowledge.+1
please add source of new Set?
great you just banged the question
@lalithkumar MDN has all the answers: Set(), and your most likely next question will be about the Spread operator
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>

Comments

1

You can reduce the 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)

Comments

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

}

1 Comment

Is that really Javascript? Looks more like Java to me.
-2

Try this

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 Comments

Yes, if repetition is not an issue then this will work
Well, i guess you didn't read the question because if you look at the example, the op want's to remove duplications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.