Delete a KEY Name in javascript - javascript

I have something like this.
"data": {
"abcd": [
{
"abc": {
"fr": "India",
"to": "Moon",
}
},
{
"abc": {
"fr": "Mars",
"to": "Jupiter",
}
}
],
"distance": 1234,
}
But I want to convert to something like this
"data": {
"abcd": [
{
"fr": "India",
"to": "Moon",
},
{
"fr": "Mars",
"to": "Jupiter",
}
],
"distance": 1234,
}
I want to brign the details inside the abc object and make them as an unnamed object inside the abcd array of objects.
Please help out with this.

A simple reduce() will suffice
let obj = {
"data": {
"abcd": [{
"abc": {
"fr": "India",
"to": "Moon",
}
},
{
"abc": {
"fr": "Mars",
"to": "Jupiter",
}
}
],
"distance": 1234,
}
};
obj.data.abcd = obj.data.abcd.reduce((a, b) => a.concat(b.abc), []);
console.log(obj);

Use Array#map to iterate the array and return the contents of the abc key of each object. Assign the results to obj.data.abcd:
const obj = {"data":{"abcd":[{"abc":{"fr":"India","to":"Moon"}},{"abc":{"fr":"Mars","to":"Jupiter"}}],"distance":1234}};
obj.data.abcd = obj.data.abcd.map((o) => o.abc);
console.log(obj);

Related

How to get a value in a JavaScript object by key

I have object and I would like to get the value of the id somehow by providing type = "EXTRA". Could someone help with this?
{
"packages": [
{
"id": "123",
"type": "EXTRA",
"name": "text",
"extras": [
{
"test": "1",
"test": "2"
}
]
},
{
"id": "234",
"type": "BASE",
"name": "text2",
"extras": [],
}
]
}
Your array
var arr = {
"packages": [
{
"id": "123",
"type": "EXTRA",
"name": "text",
"extras": [
{
"test": "1",
"test": "2"
}
]
},
{
"id": "234",
"type": "BASE",
"name": "text2",
"extras": [],
}
]
}
You can use the find() method for what you need providing the 'EXTRA' value of type
var result = arr.packages.find(x => x.type === 'EXTRA');
Your Output
console.log(result.id); //123
https://jsfiddle.net/kenpy/1moru9y4/9/
To get the value of id, use this bro and keep a name of all objects bro : objectName.packages[nameofobject.id]

How to compare two objects with lodash and return new object?

Here are two objects I need to compare and return specific values from object1 if found in object2.
object1 = {
"body": {
"items": [
{
"data": {
"name": "Smith",
"status": "Ready",
"userinfo": [
{
"dob": "01/01/2000",
"nickname": "Joe"
}
]
},
"workinfo": {
"company": "mycompany",
"address": "101 Main str."
}
},
{
"data": {
"name": "Luke",
"status": "Ready",
"userinfo": [
{
"dob": "01/01/2001",
"nickname": "LL"
}
]
},
"workinfo": {
"company": "mycompany",
"address": "101 Main str."
}
}
]
}
}
Object2 is even simple one:
object2 = {
"items": [
{
"name": "Smith",
"status": "Ready"
},
{
"name": "Luke",
"status": "Ready"
}
]
}
So if Object1 body.items[x].data.name found in Object2 items.name then finally I need to get new object like this:
object3 = {{name: "Smith", status: "Ready"}, {name: "Luke", status: "Ready"}}
You can use filter and find:
var obj1 = {
"body": {
"items": [{
"data": {
"name": "Smith",
"status": "Ready",
"userinfo": [{
"dob": "01/01/2000",
"nickname": "Joe"
}]
},
"workinfo": {
"company": "mycompany",
"address": "101 Main str."
}
},
{
"data": {
"name": "Luke",
"status": "Ready",
"userinfo": [{
"dob": "01/01/2001",
"nickname": "LL"
}]
},
"workinfo": {
"company": "mycompany",
"address": "101 Main str."
}
}
]
}
}
var obj2 = {
"items": [{
"name": "Smith",
"status": "Ready"
},
{
"name": "Luke",
"status": "Ready"
}
]
}
var output = obj2.items.filter(({name}) => obj1.body.items.find(({data}) => name === data.name))
console.log(output)
Filter will return all the objects that pass the find condition, that is, if the name is found in obj1
You can use _.intersectionWith() to return items from obj2, that their name equals data.name in obj2 items:
const obj1 = {"body":{"items":[{"data":{"name":"Smith","status":"Ready","userinfo":[{"dob":"01/01/2000","nickname":"Joe"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}},{"data":{"name":"Luke","status":"Ready","userinfo":[{"dob":"01/01/2001","nickname":"LL"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}}]}}
const obj2 = {"items":[{"name":"Smith","status":"Ready"},{"name":"Luke","status":"Ready"}]}
const result = _.intersectionWith(obj2.items, obj1.body.items,
(a, b) => _.get(a, 'name') === _.get(b, 'data.name')
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
With ES6 this is an easy task with Array.filter and Array.some:
const obj1 = {"body":{"items":[{"data":{"name":"Smith","status":"Ready","userinfo":[{"dob":"01/01/2000","nickname":"Joe"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}},{"data":{"name":"Luke","status":"Ready","userinfo":[{"dob":"01/01/2001","nickname":"LL"}]},"workinfo":{"company":"mycompany","address":"101 Main str."}}]}}
const obj2 = {"items":[{"name":"Smith","status":"Ready"},{"name":"Luke","status":"Ready"}]}
const r = obj2.items.filter(x => obj1.body.items.some(y => y.data.name == x.name))
console.log(r)
Since you have the objects in the correct layout in obj2 you can start from there and filter them against obj1.

Remove json object key value using angular

I have json response and I want to remove few object key values from it and store the edited response on other part so that I can use again.
I know by using simple javascript, but I don't have any idea in angularjs.
Json response
{
"$id": "1",
"XYZ": [],
"ABC": [
{
"$id": "41",
"ID": 1,
"Order": 0,
"Delay": 0,
"Name": "abc",
"Count": "9",
"Storage": 3,
"Groups": []
}
],
"Projected": 2019
}
Now from this Json file I want to filter out
"$id": "41","ID": 1,"Order": 0,
"Delay": 0, "Groups": [], "Name": "abc"
So my new json structure will be like this which I want to store:
{
"$id": "1",
"XYZ": [],
"ABC": [
{
"Count": "9",
"Storage": 3
}
],
"Projected": 2019
}
Any method to achieve ?
You don't need some magic angular stuff. You can just use plain old JavaScript.
My apporach iterates through all the items in the ABC array and deletes all properties defined in the props array. Note, that this actively modifies the ABC array items.
const obj = {
"$id": "1",
"XYZ": [],
"ABC": [
{
"$id": "41",
"ID": 1,
"Order": 0,
"Delay": 0,
"Name": "abc",
"Count": "9",
"Storage": 3,
"Groups": []
}
],
"Projected": 2019
}
// Now from this Json file I want to filter out
const props = ["$id", "ID", "Order", "Delay", "Groups", "Name"];
props.forEach(prop => {
obj.ABC.forEach(abc => {
delete abc[prop];
});
});
console.log(obj);
An alternative to the other solutions.
If we have a variable called json.
This method is simple
let len = json.ABC.length;
for (let i=0;i<len;i++){
delete json.ABC[i].$id;
delete json.ABC[i].ID;
delete json.ABC[i].Order;
delete json.ABC[i].Delay;
delete json.ABC[i].Groups;
delete json.ABC[i].Name;
}
try this
let json = {
"$id": "1",
"XYZ": [],
"ABC": [
{
"$id": "41",
"ID": 1,
"Order": 0,
"Delay": 0,
"Name": "abc",
"Count": "9",
"Storage": 3,
"Groups": []
}
],
"Projected": 2019
};
json["ABC"] = json["ABC"].map(obj => ({
"Count": obj["Count"],
"Storage": obj["Storage"]
}));
// or dynamic way
let keepkeys = ["Storage", "Count"];
json["ABC"] = json["ABC"].map(obj => {
let newObj = {};
keepkeys.forEach(key => newObj[key] = obj[key]);
return newObj;
});
console.log(json)

How to get the corresponding value from two objects

I have first data object which has a list of cafe, and second data object which has a list of cafe types.
I need find, get and display the corresponding type value from first data object and ID value from second data object.
For example: in list of cafe, I have Pinta with "type" : "3", it means that 3 is Bar from second object.
First object:
{
"list": {
"item": [
{
"ID": "31",
"name": "Staut",
"type": "1",
},
{
"ID": "34",
"name": "Pinta",
"type": "3",
}
]
}
}
And second object:
{
"list": {
"item": [
{
"ID": "1",
"name": "Restaurant",
},
{
"ID": "2",
"name": "Cafe",
},
{
"ID": "3",
"name": "Bar",
}
]
}
}
I can do it with Lodash. It is right, but I can't display it and it uses high memory.
getValues: function() {
_.forEach(CafeJSON.list.item, function(cafeValue) {
_.forEach(TypeJSON.list.item, function(typeValue){
if (cafeValue.type == typeValue.ID) {
console.log("Cafe name is: ", cafeValue.name, "and type is: ", typeValue.name)
}
})
})
}
Result:
I'd simplify the types object down to a object having key value pairs in the form of '3': 'Bar', then loop the items once, overriding the type property's value.
let list = {
"list": {
"item": [{
"ID": "31",
"name": "Staut",
"type": "1",
},
{
"ID": "34",
"name": "Pinta",
"type": "3",
}
]
}
}
let types = {
"list": {
"item": [{
"ID": "1",
"name": "Restaurant",
},
{
"ID": "2",
"name": "Cafe",
},
{
"ID": "3",
"name": "Bar",
}
]
}
}
let typesSimplified = types.list.item.reduce((a, b) => {
a[b.ID] = b.name;
return a;
}, {});
list.list.item.forEach(e => {
e.type = typesSimplified[e.type];
});
console.log(list);

Javascript(NodeJs) - Conversion of Arrays and Objects to bulk insert in mysql(Sequelize)

This is my json:
{
"senderName": "ifelse",
"message": "Hi",
"groups": [
{
"id": 14,
"groupname": "Angular",
"contactgroups": [
{
"id": 1,
"contact": {
"id": 1,
"gsm": "123456789"
}
},
{
"id": 3,
"contact": {
"id": 2,
"gsm": "111111111"
}
}],
"select": true
}],
"draftData": {
"contacts": [
]
}
}
How to make the above json into:
[{phoneno: 123456789; sender: ifelse ; message: Hi},{phoneno: 11111111; sender: ifelse ; message: Hi}]
I want to take phoneno data from gsm object key
Which is best method to do this? for or forEach or anyother?
I guess, this is what you want. Use map to convert contactgroups to new array with phoneno.
var data = {
"senderName": "ifelse",
"message": "Hi",
"groups": [{
"id": 14,
"groupname": "Angular",
"contactgroups": [{
"id": 1,
"contact": {
"id": 1,
"gsm": "123456789"
}
},
{
"id": 3,
"contact": {
"id": 2,
"gsm": "111111111"
}
}
],
"select": true
}],
"draftData": {
"contacts": []
}
}
var result = data.groups[0].contactgroups.map(i => {
return {
phoneno: i.contact.gsm,
sender: data.senderName,
message: data.message
}
})
console.log(result);

Categories