Use array as sort order - javascript

I want to use an array of strings as template how to order other arrays.
var sort = ["this","is","my","custom","order"];
and then i want to sort an array of objects depending on a key (content) by that order:
var myObjects = [
{"id":1,"content":"is"},
{"id":2,"content":"my"},
{"id":3,"content":"this"},
{"id":4,"content":"custom"},
{"id":5,"content":"order"}
];
so that my result is:
sortedObject = [
{"id":3,"content":"this"},
{"id":1,"content":"is"},
{"id":2,"content":"my"},
{"id":4,"content":"custom"},
{"id":5,"content":"order"}
];
how would i do that?

You can do something like this with help of sort() and indexOf()
var sort = ["this", "is", "my", "custom", "order"];
var myObjects = [{
"id": 1,
"content": "is"
}, {
"id": 2,
"content": "my"
}, {
"id": 3,
"content": "this"
}, {
"id": 4,
"content": "custom"
}, {
"id": 5,
"content": "order"
}];
var sortedObj = myObjects.sort(function(a, b) {
return sort.indexOf(a.content) - sort.indexOf(b.content);
});
document.write('<pre>' + JSON.stringify(sortedObj, null, 3) + '</pre>');

you need to use .map
var sort = ["this", "is", "my", "custom", "order"];
var myObjects = [{
"id": 1,
"content": "is"
}, {
"id": 2,
"content": "my"
}, {
"id": 3,
"content": "this"
}, {
"id": 4,
"content": "custom"
}, {
"id": 5,
"content": "order"
}];
var myObjectsSort = sort.map(function(e, i) {
for (var i = 0; i < myObjects.length; ++i) {
if (myObjects[i].content == e)
return myObjects[i];
}
});
document.write('<pre>' + JSON.stringify(myObjectsSort , null, 3) + '</pre>');

Create a new array and place the every object from myObjects considering index of the sort
Try this:
var sort = ["this", "is", "my", "custom", "order"];
var myObjects = [{
"id": 1,
"content": "is"
}, {
"id": 2,
"content": "my"
}, {
"id": 3,
"content": "this"
}, {
"id": 4,
"content": "custom"
}, {
"id": 5,
"content": "order"
}];
var newArr = [];
myObjects.forEach(function(item) {
var index = sort.indexOf(item.content);
newArr[index] = item;
});
console.log(newArr);
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

I suggest to use an object for the storing of the sort order.
var sort = ["this", "is", "my", "custom", "order"],
sortObj = {},
myObjects = [{ "id": 1, "content": "is" }, { "id": 2, "content": "my" }, { "id": 3, "content": "this" }, { "id": 4, "content": "custom" }, { "id": 5, "content": "order" }];
sort.forEach(function (a, i) { sortObj[a] = i; });
myObjects.sort(function (a, b) {
return sortObj[ a.content] - sortObj[ b.content];
});
document.write('<pre>' + JSON.stringify(myObjects, 0, 4) + '</pre>');

Related

Copy second nested array of objects into first nested array of objects

How can I assign key value from array of object to another array object
I would like to assign the key:value pair to the existing array of object from the another array of objects.
I have check this thread but it is not working in my case.
I have tried something like this but that is not returning the desired output that I am looking for.
const DataA = {
"id": 57,
"status": true,
"options": [{ "id": 1, "name": "Type A" },
{ "id": 2, "name": "Type B" },
{ "id": 3, "name": "Type C" }]
}
const DataB = {
"id": 57,
"status": true,
"options": [{ "id": 1, "value": 10 },
{ "id": 2, "value": 20 },
{ "id": 3, "value": 30 }]
}
let result;
var A1 = DataA.options.map((v) => {
console.log(v);
result = v;
})
var A2 = DataB.options.map(v => {
result.options = v;
console.log("result",result);
})
let arr3 = DataA.options.map((item, i) => Object.assign({}, item, DataB[i]));
console.log(arr3);
Result will be I need as below:
const DataA = {
"id": 57,
"status": true,
"options": [{ "id": 1, "name": "Type A", "value": 10 },
{ "id": 2, "name": "Type B", "value": 20 },
{ "id": 3, "name": "Type C", "value": 30 }]
}
I need to merge the deep clone of the array that is slightly different from this thread.
The linked duplicate does actually address your question, but you need to adjust it to your situation and not just copy paste.
DataA.options = DataA.options.map((item, i) => Object.assign({}, item, DataB.options[i]));
but since this mutates the original DataA object anyway, you may as well just use forEach() and avoid creating the intermediate array from .map().
DataA.options.forEach((item, i) => Object.assign(item, DataB.options[i]));
Both of the above assume that the options arrays of both objects are a. of the same length, and b. sorted by id. To avoid these assumptions you can use .find() to look for matching elements instead of relying on index.
DataA.options.forEach(item =>
Object.assign(item, DataB.options.find(({ id }) => id === item.id)));
const DataA = {
"id": 57,
"status": true,
"options": [
{ "id": 1, "name": "Type A" },
{ "id": 2, "name": "Type B" },
{ "id": 3, "name": "Type C" }]
}
const DataB = {
"id": 57,
"status": true,
"options": [
{ "id": 1, "value": 10 },
{ "id": 2, "value": 20 },
{ "id": 3, "value": 30 }]
}
DataA.options.forEach(item =>
Object.assign(item, DataB.options.find(({ id }) => id === item.id)));
console.log(DataA)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Get all key values from multi level nested array JavaScript

I have an object like this:
var data = [
{"id":"36e1e015d703120058c92cf65e6103eb","title":"Alex McGibbon"},
{"id":"60beb5e7d7600200e5982cf65e6103ad","title":"Alex Linde"},
{"subs":[{"id":"62826bf03710200044e0bfc8bcbe5df1","title":"Abel Tuter"}],"id":"63e8479fdb161300bde15901cf96191c","title":"Abdul Waheed"},
{"subs":[{"subs":[{"id":"12826bf03710200044e0bfc8bcbe5db1","title":"Alfonso Griglen"},{"subs":[{"id":"06826bf03710200044e0bfc8bcbe5d8a","title":"Allyson Gillispie"},{"id":"b282abf03710200044e0bfc8bcbe5d28","title":"Allan Schwantd"}],"id":"22826bf03710200044e0bfc8bcbe5dec","title":"Alejandra Prenatt"}],"id":"0a826bf03710200044e0bfc8bcbe5d7a","title":"Adela Cervantsz"},{"id":"4847c4d4d773020058c92cf65e61038e","title":"Alisa Chinoy"},{"id":"71826bf03710200044e0bfc8bcbe5d3b","title":"Aileen Mottern "},{"id":"a8f98bb0eb32010045e1a5115206fe3a","title":"Abraham Lincoln"}],"id":"7c2e6109dbd65300bde15901cf9619b5","title":"Raju Koyagura"}
];
console.log(data)
Now I want to retrieve all the id values as a new array without consideration of which nested level it is.
My expected result is something like this::
var result = ['36e1e015d703120058c92cf65e6103eb','60beb5e7d7600200e5982cf65e6103ad','62826bf03710200044e0bfc8bcbe5df1','06826bf03710200044e0bfc8bcbe5d8a','b282abf03710200044e0bfc8bcbe5d28','22826bf03710200044e0bfc8bcbe5dec','0a826bf03710200044e0bfc8bcbe5d7a','4847c4d4d773020058c92cf65e61038e','71826bf03710200044e0bfc8bcbe5d3b','a8f98bb0eb32010045e1a5115206fe3a','7c2e6109dbd65300bde15901cf9619b5'];
console.log(result);
I am not getting any idea how to achieve it.?
You can use JSON.stringify to walk on the tree easily:
const ids = [];
JSON.stringify(data, (key, value) => {
if (key === 'id') ids.push(value);
return value;
});
Create a recursive function and check if that object have a key by id. Push the value of id. If the key is another array then call the same function with new value
var data = [{
"id": "36e1e015d703120058c92cf65e6103eb",
"title": "Alex McGibbon"
},
{
"id": "60beb5e7d7600200e5982cf65e6103ad",
"title": "Alex Linde"
},
{
"subs": [{
"id": "62826bf03710200044e0bfc8bcbe5df1",
"title": "Abel Tuter"
}],
"id": "63e8479fdb161300bde15901cf96191c",
"title": "Abdul Waheed"
},
{
"subs": [{
"subs": [{
"id": "12826bf03710200044e0bfc8bcbe5db1",
"title": "Alfonso Griglen"
}, {
"subs": [{
"id": "06826bf03710200044e0bfc8bcbe5d8a",
"title": "Allyson Gillispie"
}, {
"id": "b282abf03710200044e0bfc8bcbe5d28",
"title": "Allan Schwantd"
}],
"id": "22826bf03710200044e0bfc8bcbe5dec",
"title": "Alejandra Prenatt"
}],
"id": "0a826bf03710200044e0bfc8bcbe5d7a",
"title": "Adela Cervantsz"
}, {
"id": "4847c4d4d773020058c92cf65e61038e",
"title": "Alisa Chinoy"
}, {
"id": "71826bf03710200044e0bfc8bcbe5d3b",
"title": "Aileen Mottern "
}, {
"id": "a8f98bb0eb32010045e1a5115206fe3a",
"title": "Abraham Lincoln"
}],
"id": "7c2e6109dbd65300bde15901cf9619b5",
"title": "Raju Koyagura"
}
];
let newArray = [];
function getAllId(arr, key) {
arr.forEach(function(item) {
for (let keys in item) {
if (keys === key) {
newArray.push(item[key])
} else if (Array.isArray(item[keys])) {
getAllId(item[keys], key)
}
}
})
}
getAllId(data, 'id')
console.log(newArray)

Filter multiple objects from array

I'm trying to filter an array of objects where the filter is another array (of integers) which are values of properties of the first array. I've managed to make it work but I'm not sure if it's the best way. Since I'm a beginner in javascript, I'd appreciate any suggestions/improvements.
The items.json file contains an object with an array of objects. I want to filter all the objects (within that array) that have an id equal to the "ids" on the itemsids array.
code:
const itemsall = require('./items.json');
let itemsids = [1, 403, 3];
let filtereditems = [];
itemsids.forEach(id => {
itemsall.items.forEach(item => {
if (id === item.id) {
filtereditems.push(item);
}
});
});
items.json (a small part of it)
{
"items": [
{
"id": 0,
"name": "Egg",
"img": "http://www.serebii.net/pokemongo/items/egg.png"
},
{
"id": 1,
"name": "Pokeball",
"img": "http://www.serebii.net/pokemongo/items/20pokeballs.png"
},
{
"id": 2,
"name": "Greatball",
"img": "http://www.serebii.net/pokemongo/items/greatball.png"
}
]
}
output: (expected)
[
{
"id": 0,
"name": "Egg",
"img": "http://www.serebii.net/pokemongo/items/egg.png"
},
{
"id": 403,
"name": "Cool Incense",
"img": "http://www.serebii.net/pokemongo/items/incense.png"
},
{
"id": 3,
"name": "Ultraball",
"img": "http://www.serebii.net/pokemongo/items/ultraball.png"
}
]
Thanks!
You can use filter() and indexOf() to return filtered array.
var data = {
"items": [{
"id": 0,
"name": "Egg",
"img": "http://www.serebii.net/pokemongo/items/egg.png"
}, {
"id": 1,
"name": "Pokeball",
"img": "http://www.serebii.net/pokemongo/items/20pokeballs.png"
}, {
"id": 2,
"name": "Greatball",
"img": "http://www.serebii.net/pokemongo/items/greatball.png"
}]
}
let itemsids = [1, 403, 3];
var result = data.items.filter(function(e) {
return itemsids.indexOf(e.id) != -1
})
console.log(result)
With ES6/ES7 you can use includes() like this.
var result = data.items.filter((e) => itemsids.includes(e.id));

Merging collections to json array

I have 2 json format, namely jsonA and jsonB, in which i would like to combine into jsonC as follow.
Appreciate if someone can tell me how to do it.
jsonA
{
"text1": "Hello",
"text2": "Hi",
"text3": "There"
}
jsonB
[
{
"id": "text1",
"category": "Big"
},
{
"id": "text2",
"category": "Medium"
},
{
"id": "text3",
"category": "Small"
},
]
Final
[
{
"id": "text1",
"category": "Big",
"message": "Hello"
},
{
"id": "text2",
"category": "Medium",
"message": "Hi"
},
{
"id": "text3",
"category": "Small",
"message": "There"
}
]
Solution for a new array.
Basically it iterates the array and build a new object for every found object. It adds a new property message with the wanted content form objectA.
var objectA = { "text1": "Hello", "text2": "Hi", "text3": "There" },
objectB = [{ "id": "text1", "category": "Big" }, { "id": "text2", "category": "Medium" }, { "id": "text3", "category": "Small" }],
objectC = objectB.map(function (a) {
return {
id: a.id,
category: a.category,
message: objectA[a.id]
};
});
document.write('<pre>' + JSON.stringify(objectC, 0, 4) + '</pre>');
Soulution for a mutated array.
This solution takes the array and adds in the loop a new property with the value from objectA.
var objectA = { "text1": "Hello", "text2": "Hi", "text3": "There" },
objectB = [{ "id": "text1", "category": "Big" }, { "id": "text2", "category": "Medium" }, { "id": "text3", "category": "Small" }];
objectB.forEach(function (a) {
a.message = objectA[a.id];
});
document.write('<pre>' + JSON.stringify(objectB, 0, 4) + '</pre>');
Firstly, You need to traverse 2nd json array. Than traverse 1st json and compare for indexing. If indexing matches then add new attribute to 2nd json.
Here is full example :
var jsonA = '{"text1": "Hello","text2": "Hi","text3": "There"}';
var jsonB = '[{"id": "text1","category": "Big"},{"id": "text1","category": "Medium"},{"id": "text1","category": "Small"}]';
jsonA = JSON.parse(jsonA);
jsonB = JSON.parse(jsonB);
for(var i=0;i<jsonB.length;i++){
for(var j in jsonA){
if(j == ("text"+(i+1))){
jsonB[i].message = jsonA[j];
}
}
}
console.log(JSON.stringify(jsonB));
Output : [{"id":"text1","category":"Big","message":"Hello"},{"id":"text1","category":"Medium","message":"Hi"},{"id":"text1","category":"Small","message":"There"}]

JS string counter

I want this code to count the number of times that category and message are in the bottom string, regardles of the different id number. The code just gives me the last one.
It is quite messy, sry. Thanks for your help.
Kind regards.
counter = function() {
var value = $('#text').val();
if (value.length == 0) {
$('#wordCount').html(0);
return;
}
var regex = /\s+/gi;
var wordCount = value.trim().replace(regex, ' ').split(' ').length;
};
$(document).ready(function() {
var obj = {"data" : {
"from": {
"category": "cat1",
"id": 1,
"message": "WIZ1"
},
"from": {
"category": "cat2",
"id": 2,
"message": "WIZ2"
},
"from": {
"category": "cat3",
"id": 3,
"message": "WIZ3"
},
"from": {
"category": "cat4",
"id": 4,
"message": "WIZ3"
},
}
};
$.each(obj, function() {
$.each(this, function(name, value) {
$("#result").append(name + '=' + value.category);
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result">Words: <span id="wordCount">0</span>
<br/>
</div>
You have a single object with the "from" property being overwritten 4 times. It looks like what you wanted was to instead have an array, like the example below.
I also changed the code to actually capture the categories and messages, like the description in your post, after the loops those objects will have the count by message and count by category.
counter = function() {
var value = $('#text').val();
if (value.length == 0) {
$('#wordCount').html(0);
return;
}
var regex = /\s+/gi;
var wordCount = value.trim().replace(regex, ' ').split(' ').length;
};
$(document).ready(function() {
var obj = {"data" : [
{
"category": "cat1",
"id": 1,
"message": "WIZ1"
},
{
"category": "cat2",
"id": 2,
"message": "WIZ2"
},
{
"category": "cat3",
"id": 3,
"message": "WIZ3"
},
{
"category": "cat4",
"id": 4,
"message": "WIZ3"
},
]
};
var categories = {};
var messages = {}
$.each(obj, function() {
$.each(this, function(name, value) {
if (!categories[value.category]) categories[value.category] = 0;
categories[value.category] += 1;
if (!messages[value.message]) messages[value.message] = 0;
messages[value.message] += 1;
$("#result").append(name + '=' + value.category);
});
});
console.log(categories);
console.log(messages);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result">Words: <span id="wordCount">0</span>
<br/>
</div>
Each of the items in the JS object have the same key ('from').
That's causing each one to overwrite the previous, which is why it's only finding the last one.
{
"data": {
"from1": {
"category": "cat1",
"id": 1,
"message": "WIZ1"
},
"from2": {
"category": "cat2",
"id": 2,
"message": "WIZ2"
},
"from3": {
"category": "cat3",
"id": 3,
"message": "WIZ3"
},
"from4": {
"category": "cat4",
"id": 4,
"message": "WIZ3"
}
}
}
This will allow it to iterate through each item as there will now be four items present.

Categories