Separate string into a set of objects - javascript

I have this string var string = "{name: Hello, age: 20}, {name: Nadia, age: 30}" returned from the backend. I created an object out of it by appending it to an empty array like so:
var array = [];
var array1 = array.push(string);
After appending to an array, I got the following result:
Array [
"{name: Hello, age: 20},
{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}"
]
My intended result is this:
Array [
"{name: Hello, age: 20}",
"{name: Nadia, age: 30}",
"{name: Nadia, age: 30}",
"{name: Nadia, age: 30}",
"{name: Nadia, age: 30}"
]
I have tried using split(",") but they're separated individually instead. How can I achieve the intended result? I'm very new to this.
UPDATE: The answer provided gave out not quite the result that I wanted. I have this larger set of objects. The solution returns me with 2 objects, with the second object being a combination of objects. Here's the output using the method in the answer:
Array [
"{name: Hello, age: 20}",
"{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}
{name: Nadia, age: 30}"
]
Also, I have asked my fellow backend dev to fix the json return, sadly they cannot change it. I'm not sure why. The only way I can handle this is on frontend.

Below method works but its a bit crude. But you can give it a try..
let str = "{name: Hello, age: 20}, {name: Nadia, age: 30}, {name: Nadia, age: 30}, {name: Nadia, age: 30}";
let x = str.replaceAll('}, ', '}/').split('/');
console.log(x);

Related

How to filter an array of objects by another object? [duplicate]

This question already has answers here:
Filter array of objects with another array of objects
(11 answers)
Closed 9 months ago.
Like i have an array of objects:
const arr = [
{name: 'Adam', age: 23},
{name: 'Steve', age: 23},
{name: 'Eve', age: 30}
];
And i have another object
let obj = {age: 23}
How can I filter an array so it will be contain just:
[
{name: 'Adam', age: 23},
{name: 'Steve', age: 23},
];
let newArray = arr.filter(a => a.age == obj.age);

Create an array of objects?

arr = [
men: {name: "john", age:"30"},{name: "john2", age:"31"},
women: {name: "kim", age:"10"},{name: "karen", age:"33"}
]
I'm looking to create an array with properties men and women, and each of those will have a bunch of objects. How can I do that in JS?
First create object then array in it, so like this you can manage your data.
let arr = {
men: [
{name: 'john', age: 30},
{name: 'john2', age:33}
],
women: [
{name: 'kim', age: 30},
{name: 'kim2', age:33}
],
}
console.log( arr.men[0].name );
just extend the value in the men or women array.
Thanks!
You need an object in order to achieve this. After this, you will be able to manage the people object just anyhow you want.
var people = {
men: [
{name: "man1", age: 30},
{name: "man2", age: 31},
],
women: [
{name: "woman1", age: 30},
{name: "woman2", age: 31},
]
}
// Add to men
people.men.push({"man3", age: 32})
// Add to women
people.women.push({"woman3", age: 32});
// Print the people object
console.log(JSON.stringify(people, null, 2)); // pretty format

An array of objects that checks each object age and then returns the name of all objects that meet that age

I had this question on an exam and I got it wrong and I have been trying my best reading through MDN Web Docs but being only a week into learning JavaScript I have no clue what I am looking for.
This what I can recall from my exam, it this fictional state only 15-year-olds and above can be babysitters. I had used a ".find()" and I only could produce 1 person and it gave me the whole object and they just wanted the name. Gave a day in a half to find the answer myself but I am lost. HELP!
If you also have a link so I can read up more on this it will be helpful, because other students said they had similar and they asked them to average all the age instead. Arrays and Objects have me wanting to bang my head against the wall already.
function legalBabysitter(sitters){
};
/*Do not modify anything below this line*/
const babysitterArray = [
{name: 'Peter', age: 13},
{name: 'Paul', age: 15},
{name: 'Mary', age: 17}
];
console.log(legalBabysitter(babysitterArray)); // should produce [Paul, Mary]
You can use fitler and map like this return sitters.filter(c=>c.age >= 15).map(c=>c.name);
function legalBabysitter(sitters){
return sitters.filter(c=>c.age >= 15).map(c=>c.name);
};
/*Do not modify anything below this line*/
const babysitterArray = [
{name: 'Peter', age: 13},
{name: 'Paul', age: 15},
{name: 'Mary', age: 17}
];
console.log(legalBabysitter(babysitterArray)); // should produce [Paul, Mary]
If you need only for exam, you can use for loop, it is very clear.
function legalBabysitter(sitters){
var result = [];
for(var i =0; i < sitters.length; i ++){
if(sitters[i].age >= 15){
result.push(sitters[i].name);
}
}
return result;
};
function legalBabysitter(sitters){
var result = [];
for(var i =0; i < sitters.length; i ++){
if(sitters[i].age >= 15){
result.push(sitters[i].name);
}
}
return result;
};
/*Do not modify anything below this line*/
const babysitterArray = [
{name: 'Peter', age: 13},
{name: 'Paul', age: 15},
{name: 'Mary', age: 17}
];
console.log(legalBabysitter(babysitterArray)); // should produce [Paul, Mary]
const babysitterArray = [
{name: 'Peter', age: 13},
{name: 'Paul', age: 15},
{name: 'Mary', age: 17}
];
function legalBabysitter(sitters){
return sitters.filter(_ => _.age > 14).map(_ => _.name)
};
console.log(legalBabysitter(babysitterArray))
If you don't know .filer and .reduce, you can do it by .forEach.
const babysitterArray = [
{name: 'Peter', age: 13},
{name: 'Paul', age: 15},
{name: 'Mary', age: 17}
];
function legalBabysitter(sitters){
let target = [];
sitters.forEach(_ => {
if (_.age > 14) target.push(_.name)
})
return target;
};
console.log(legalBabysitter(babysitterArray))
You can use reduce
Here idea is
Loop through array
Check if age is greater than or equal to 15 than add name to op
else keep it as it is
return op from function
function legalBabysitter(sitters){
return sitters.reduce((op,inp)=>{
if(inp.age >= 15){
op.push(inp.name)
}
return op
},[])
};
/*Do not modify anything below this line*/
const babysitterArray = [
{name: 'Peter', age: 13},
{name: 'Paul', age: 15},
{name: 'Mary', age: 17}
];
console.log(legalBabysitter(babysitterArray)); //

What is smallest way to split object into array of object based on key/value pairs in Javascript?

I've got this object:
var obj = {
family : [{name: 'will', age: 30}, {name: 'husain', age: 12}],
friends : [{name: 'cody', age: 31}, {name: 'jeff', age: 11}],
school : [{name: 'daniel', age: 20}, {name: 'carl', age: 15}]
}
convert it into this
var obj = [
{family : [{name: 'will', age: 30}, {name: 'husain', age: 12}]},
{friends : [{name: 'cody', age: 31}, {name: 'jeff', age: 11}]},
{school : [{name: 'daniel', age: 20}, {name: 'carl', age: 15}]}
];
Write now I am using for..in to build a new array and create object with key as key for new object and so on.
I'm doing this right now
var arr = [];
for (let key in obj) {
arr.push({key: obj[key]})
}
I think Object.keys is your best option:
var obj = {
family : [{name: 'will', age: 30}, {name: 'husain', age: 12}],
friends : [{name: 'cody', age: 31}, {name: 'jeff', age: 11}],
school : [{name: 'daniel', age: 20}, {name: 'carl', age: 15}]
}
var r = Object.keys(obj).map(x => ({[x]: obj[x]}) )
console.log(r)

How to implement filter in strict mode in ng-grid?

I'm Using ng-grid to populate the Data. I'm using the ng-grid internal filter. But its not what i want.
Data is
$scope.myData = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
If I search "Moroni" I'm getting the output i needed. But If i search "roni" ( Moroni ) I should not see Moroni name. My question is how can i apply the strict comparison of the key value.
I want the filter should be true only when
filterText == 'Moroni' [ exactly matching the text instead substring ]
In the angular docs for filter: http://docs.angularjs.org/api/ng.filter:filter
You find this under comparator:
true: A shorthand for function(actual, expected) { return angular.equals(expected, actual)}.
this is essentially strict comparison of expected and actual.
So try filter:filterText:true

Categories