Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am using typescript.
I want to create a new array that compares the code values of the arrays groupA and groupB and adds same:true to the same codes and same:false to the ones that are not the same. I want to combine the same codes into one.
I want to create an array like newGroup.
"groupA": [
{
"code": "aaaaa",
"name": "group1",
"editable": false,
},
{
"code": "bbbb",
"name": "group2",
"editable": false,
},
{
"code": "cccc",
"name": "group3",
"editable": false,
},
{
"code": "dddd",
"name": "group4",
"editable": false,
},
{
"code": "eeee",
"name": "group5",
"editable": false,
},
]
"groupB": [
{
"code": "aaaaa",
"name": "group1",
},
{
"code": "bbbb",
"name": "group2",
},
]
"newGroup": [
{
"code": "aaaaa",
"name": "group1",
"editable": false,
"same":true,
},
{
"code": "bbbb",
"name": "group2",
"editable": false,
"same":true,
},
{
"code": "cccc",
"name": "group3",
"editable": false,
"same":false,
},
{
"code": "dddd",
"name": "group4",
"editable": false,
"same":false,
},
{
"code": "eeee",
"name": "group5",
"editable": false,
"same":false,
},
]
const groupA =[
{
"code": "aaaaa",
"name": "group1",
"editable": false,
},
{
"code": "bbbb",
"name": "group2",
"editable": false,
},
{
"code": "cccc",
"name": "group3",
"editable": false,
},
{
"code": "dddd",
"name": "group4",
"editable": false,
},
{
"code": "eeee",
"name": "group5",
"editable": false,
}
]
const groupB=[
{
"code": "aaaaa",
"name": "group1",
},
{
"code": "bbbb",
"name": "group2",
}
] ;
const newGroup = groupA.map(a => {
const found = groupB.find(b=> (b.code===a.code && b.name===a.name));
return {...a,...found,same:!!found};
})
console.log(newGroup);
Related
I was able to find many sources saying how to filter nested arrays, but all of these referred to arrays within arrays. In my case, there's an array nested within an object and I can't figure out how to deal with it.
The source array is:
{
"msg": "OK",
"blueprint": {
"result": "OK",
"blueprint": {
"id": "a2e63ee01401aaeca78be023dfbb8c59",
"product": {
"productName": "Test Product",
"productId": "AS_12-01",
"description": "Test Descr.",
"childProducts": [
{
"childId": "T1",
"parent": "8c59"
},
{
"childId": "T5",
"parent": "8c7e"
}
],
"components": [
{
"compId": "C2", #
"leadTime": 21, # remove
"available": false #
},
{
"compId": "C5",
"leadTime": 3,
"available": true
},
{
"compId": "C6", #
"leadTime": 12, # remove
"available": false #
},
{
"compId": "C8",
"leadTime": 5,
"available": true
},
]
},
"owner": "dummy",
"name": "du_test"
}
}
}
How to filter the nested array so that the resulting object looks like:
{
"msg": "OK",
"blueprint": {
"result": "OK",
"blueprint": {
"id": "a2e63ee01401aaeca78be023dfbb8c59",
"product": {
"productName": "Test Product",
"productId": "AS_12-01",
"description": "Test Descr.",
"childProducts": [
{
"childId": "T1",
"parent": "8c59"
},
{
"childId": "T5",
"parent": "8c7e"
}
],
"components": [
{
"compId": "C5",
"leadTime": 3,
"available": true
},
{
"compId": "C8",
"leadTime": 5,
"available": true
},
]
},
"owner": "dummy",
"name": "du_test"
}
}
}
So, basically the structure is the same, only the nested array has the unavailable objects removed.
How to achieve it?
you can simply reassign the field with the filtered array
const x = { "msg": "OK", "blueprint": { "result": "OK", "blueprint": { "id": "a2e63ee01401aaeca78be023dfbb8c59", "product": { "productName": "Test Product", "productId": "AS_12-01", "description": "Test Descr.", "childProducts": [ { "childId": "T1", "parent": "8c59" }, { "childId": "T5", "parent": "8c7e" } ], "components": [ { "compId": "C2", "leadTime": 21, "available": false }, { "compId": "C5", "leadTime": 3, "available": true }, { "compId": "C6", "leadTime": 12, "available": false }, { "compId": "C8", "leadTime": 5, "available": true }, ] }, "owner": "dummy", "name": "du_test" } }}
x.blueprint.blueprint.product.components = x.blueprint.blueprint.product.components.filter(({available}) => available)
console.log(x)
I'm trying to manipulate data received from an API, it requires grouping according to the category like country, city and state based on their display_priority. Also, sort similar category items based on their display_priority. I did try using JS functions and also with lodash library, but couldn't get the desired output.
API response:
[
{
"category": {
"name": "country",
"display_priority": 1
},
"name": "US",
"display_priority": 2,
"enabled": false
},
{
"category": {
"name": "city",
"display_priority": 3
},
"name": "Greenville",
"display_priority": 1,
"enabled": true
},
{
"category": {
"name": "state",
"display_priority": 2
},
"name": "Alabama",
"display_priority": 2,
"enabled": true
},
{
"category": {
"name": "state",
"display_priority": 2
},
"name": "Arizona",
"display_priority": 1,
"enabled": false
},
{
"category": {
"name": "city",
"display_priority": 3
},
"name": "Houston",
"display_priority": 2,
"enabled": false
},
{
"category": {
"name": "country",
"display_priority": 1
},
"name": "Germany",
"display_priority": 1,
"enabled": true
}
]
Required output:
[
{
"category": "country",
"list": [
{
"name": "Germany",
"enabled": true
},
{
"name": "US",
"enabled": false
}
]
},
{
"category": "state",
"list": [
{
"name": "Arizona",
"enabled": false
},
{
"name": "Alabama",
"enabled": true
}
]
},
{
"category": "city",
"list": [
{
"name": "Greenville",
"enabled": true
},
{
"name": "Houston",
"enabled": false
}
]
}]
You can easily get this result with a single Array.reduce and object destructuring:
const data = [{ "category": { "name": "country", "display_priority": 1 }, "name": "US", "display_priority": 2, "enabled": false }, { "category": { "name": "city", "display_priority": 3 }, "name": "Greenville", "display_priority": 1, "enabled": true }, { "category": { "name": "state", "display_priority": 2 }, "name": "Alabama", "display_priority": 2, "enabled": true }, { "category": { "name": "state", "display_priority": 2 }, "name": "Arizona", "display_priority": 1, "enabled": false }, { "category": { "name": "city", "display_priority": 3 }, "name": "Houston", "display_priority": 2, "enabled": false }, { "category": { "name": "country", "display_priority": 1 }, "name": "Germany", "display_priority": 1, "enabled": true } ]
const result = data.reduce((accumulator, {category, name, enabled}) => {
let key = category.name
accumulator[key] = accumulator[key] || { category: key, list: [] }
accumulator[key].list.push({name, enabled})
return accumulator
}, {})
console.log(Object.values(result))
I am using lodash and have this json object, the property I would like to return is a nested array ie swappableProducts:
{
"existing": {
"hasPlatinum": false,
"primaryBundle": {
"id": "2008",
"serviceId": "1",
"name": "TV - Entertainment, Sport",
"products": [
{
"name": "Entertainment",
"id": "100",
"price": 2600,
"gifted": false
},
{
"name": "Sport",
"id": "107",
"price": 2500,
"gifted": false,
"swappableProducts": [
{
"name": "Movies",
"id": "105",
"price": 2000,
"gifted": false
},
{
"name": "Games",
"id": "10",
"price": 1000,
"gifted": false
}
]
}
]
}
}
I tried to return the swappableProducts array like this:
_.get(userObject, 'existing.primaryBundle.products[1].swappableProducts');
but it won't return it. How can I do this?
I have following JSON data received form REST api.
[
{
"names": {
"en": "test123"
},
"children": [],
"id": "68d87e8c-42f5-4f11-b25a-b30624246c3b",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test123",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
},
{
"names": {
"en": "test456"
},
"children": [],
"id": "d175e6d1-874e-4909-afb2-790c0a940c3f",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test456",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}
]
I'm trying to display it using directive ng-repeat. Using track by object.id.
It is used like this:
<tr ng-repeat="chapter in chapters | filter: search track by chapter.id">
But the problem is that the ngRepeat:Dupes error still appear. I have checked the data contained in JSON, but there is no duplicite id in it. Do you know why the ngRepeat:Dupes error persists?
Based on the data given, no duplicate error is thrown, below is jsfiddle.
<div ng-controller="MainCtrl">
<input type="text" ng-model="search">
<div ng-repeat="chapter in chapters | filter: search track by chapter.id">{{chapter.id}}</div>
</div>
controller
$scope.chapters = [{
"names": {
"en": "test123"
},
"children": [],
"id": "68d87e8c-42f5-4f11-b25a-b30624246c3b",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test123",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}, {
"names": {
"en": "test456"
},
"children": [],
"id": "d175e6d1-874e-4909-afb2-790c0a940c3f",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test456",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}]
http://jsfiddle.net/75sdsuz2/2/
I have this collection: https://graph.facebook.com/2playcz/albums
This collection contains 8 id. How can i get the total count id of this collection using javascript? (Total = 8)
Source:
{
"data": [
{
"id": "201936779932071",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tr\u00e9ninky",
"link": "https://www.facebook.com/album.php?fbid=201936779932071&id=190320081093741&aid=41883",
"cover_photo": "201937046598711",
"count": 8,
"type": "normal",
"created_time": "2012-07-02T09:33:43+0000",
"updated_time": "2012-09-15T12:05:44+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "1788805921",
"name": "Edita Nov\u00e1"
},
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/201936779932071/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "205206429605106",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Turnaje a akce",
"link": "https://www.facebook.com/album.php?fbid=205206429605106&id=190320081093741&aid=42900",
"cover_photo": "205208716271544",
"count": 14,
"type": "normal",
"created_time": "2012-07-10T19:36:53+0000",
"updated_time": "2012-09-15T12:04:05+0000",
"can_upload": false
},
{
"id": "221784994613916",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tenisova \u0161kolka 2play",
"description": "Tenisov\u00e1 \u0161kolka 2play",
"link": "https://www.facebook.com/album.php?fbid=221784994613916&id=190320081093741&aid=49379",
"cover_photo": "221785024613913",
"count": 9,
"type": "normal",
"created_time": "2012-08-31T11:19:59+0000",
"updated_time": "2012-09-14T15:17:53+0000",
"can_upload": false
},
{
"id": "203405996451816",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Webov\u00e9 fotografie - Logo",
"link": "https://www.facebook.com/album.php?fbid=203405996451816&id=190320081093741&aid=42285",
"cover_photo": "203406586451757",
"count": 11,
"type": "normal",
"created_time": "2012-07-05T10:12:40+0000",
"updated_time": "2012-09-14T15:16:40+0000",
"can_upload": false
},
{
"id": "190332361092513",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Cover Photos",
"link": "https://www.facebook.com/album.php?fbid=190332361092513&id=190320081093741&aid=39232",
"cover_photo": "225939404198475",
"count": 2,
"type": "normal",
"created_time": "2012-06-09T13:52:38+0000",
"updated_time": "2012-09-12T18:15:51+0000",
"can_upload": false
},
{
"id": "190802884378794",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Wall Photos",
"link": "https://www.facebook.com/album.php?fbid=190802884378794&id=190320081093741&aid=39324",
"cover_photo": "190802891045460",
"count": 2,
"type": "wall",
"created_time": "2012-06-10T13:19:48+0000",
"updated_time": "2012-07-17T17:16:19+0000",
"can_upload": false
},
{
"id": "205207126271703",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Ostatn\u00ed",
"link": "https://www.facebook.com/album.php?fbid=205207126271703&id=190320081093741&aid=42902",
"cover_photo": "205209679604781",
"count": 4,
"type": "normal",
"created_time": "2012-07-10T19:40:05+0000",
"updated_time": "2012-07-16T14:47:16+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/205207126271703/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "190320914426991",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Profile Pictures",
"link": "https://www.facebook.com/album.php?fbid=190320914426991&id=190320081093741&aid=39224",
"cover_photo": "190327474426335",
"count": 1,
"type": "profile",
"created_time": "2012-06-09T13:29:16+0000",
"updated_time": "2012-06-09T13:43:08+0000",
"can_upload": false
},
{
"id": "190322704426812",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Logo",
"description": "Loga spole\u010dnosti",
"link": "https://www.facebook.com/album.php?fbid=190322704426812&id=190320081093741&aid=39225",
"type": "normal",
"created_time": "2012-06-09T13:34:09+0000",
"updated_time": "2012-07-05T10:16:58+0000",
"can_upload": false
}
]
}
If you mean the count of items in the data array, it would just be data.length.
First of all, there 9 ids. If you want to count the number of elements in array there's a built-in property length. So that if your object is called x you retrieve the length of data by accessing x.data.length.
On the other hand, if you wanted to count the number of unique ids (IDs should by unique anyway, but if you really really wanted to) you have to manually iterate through the array and count them:
var countIds = function (arr) {
var uniqueIds = {}, num = 0;
if (!arr.data) return false;
arr.data.forEach(function (val, i) {
if (typeof uniqueIds[val.id] === "undefined") {
++num;
uniqueIds[val.id] = true;
}
});
return num;
};