Select all JSONObjects from JSONArray containing [duplicate] - javascript

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 8 years ago.
I have a JSONArray containing JSONObjects like this {firstname:'John', lastname:'Doe'}.
Is there a way to select all JSONObjects having firstname == 'John' directly or the only way is to loop the array, test the field firstname and save all matching objects in another array?
Thanks

The filter method should do the trick.
var jsonArrayString = '[{"firstname": "John","otherProp": "otherValue"},{"firstname": "other Name", "otherProp": "otherValue"}]';
var jsonArray = JSON.parse(jsonArrayString);
var filteredArray = jsonArray.filter(function(element) {
return element.firstname === "John";
});
console.log(filteredArray);
Advice: The filter method is not supported in <= IE 8. but there is a polyfill in the MDN article.

You can filter them out:
var people = JSONArray.filter(function(obj) {
return obj.firstname == "John";
});
FYI: You have an array containing objects.

Related

Find partial string in array [duplicate]

This question already has answers here:
How do you search an array for a substring match?
(15 answers)
Closed 3 years ago.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
Let's say you don't know whats inside the fruits array ?
How do you extract the string that contains mango.
The prefix string must be included in the result.
Can this be done without a for loop and parsing ?
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
console.log(n)
You need to filter the array and check each string.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"],
result = fruits.filter(string => string.includes("Mango"));
console.log(result);
How about the following?
fruits.filter(fruit => fruit.includes('Mango'))
// [ 'CarPaint.InString.Mango' ]

Remove duplicates from array within array in Javascript [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 7 years ago.
I started with an array formatted like this:
var cus = {
"acct":[
{
"latitude":"41.4903",
"longitude":"-90.56956",
"part_no":"P1140",
"no_sold":1
},
{
"latitude":"48.118625",
"longitude":"-96.1793",
"part_no":"227",
"no_sold":1
},
....
]
Next I put all of the part_no in a separate array like this:
var list = [];
$.each(cus.acct,function(index,value){
list = [value["part_no"]];
These are the results when I do a console.log() of my array:
["P1140"]
["227"]
["224"]
["600"]
.....
["756"]
["756"]
["756"]
How do I remove duplicates from this array of just part_no's with javascript/jquery? I've looked at other examples but can't find one that works for me. Take note that I'm just beginning with javascript as well.
function getUnique(arr){
var result = [];
$.each(arr, function(i, e) {
if(typeof e != "undefined")
{
if ($.inArray(e, result) == -1) result.push(e)
}
});
return result;
}
If you can use any libraries like underscope or lodash will provide more options.
I would use the jQuery unique option. It should remove any duplicates from your array.

Add multiple options in array with javascript [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
I have this var
var allwords = {};
And i need push more options in this array like:
allwords.push = {"Ctrl-Space": "autocomplete"};
allwords.push = {"Ctrl-pause": "closewindow"};
And look like this:
allwords = {"Ctrl-Space": "autocomplete", "Ctrl-pause": "closewindow"};
How can't i do?
push is for Array objects. For traditional objects, assign the properties manually like
var allwords = {};
allwords["Ctrl-Space"] = "autocomplete";
allwords["Ctrl-pause"] = "closewindow";
console.log(allwords);

Finding values of a mapping/dict [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 9 years ago.
I have map/dictionary in Javascript:
var m = {
dog: "Pluto",
duck: "Donald"
};
I know how to get the keys with Object.keys(m), but how to get the values of the Object?
You just iterate over the keys and retrieve each value:
var values = [];
for (var key in m) {
values.push(m[key]);
}
// values == ["Pluto", "Donald"]
There is no similar function for that but you can use:
var v = Object.keys(m).map(function(key){
return m[key];
});

add array value to key javascript [duplicate]

This question already has answers here:
Add new value to an existing array in JavaScript [duplicate]
(8 answers)
Closed 10 years ago.
i'm getting the data from a form when it is submitted like this
values = {};
$("#myForm").submit(function(){
$.each($('#myForm').serializeArray(), function(i, field) {
if(field.name != 'r'){
values[field.name] = field.value;
}
});
return false;
});
The problem is that i want to do that multiple times and store all the data in the var values using field.name as a keys and the values as an array
to compare it in php i would do values[field.name][] = field.value;
is there any similar syntax in js ?
Yeah, you can add multiple values using the Array.push method. But first, you must define values[field.name] as array, like this:
values[field.name] = [];
values[field.name].push(somevalue);

Categories

Resources