i want to change the index of all array elements with other array
this array came from csv file and it may be more than 3 columns
the index array is:
theheadoptions=[
"uid",
"nid",
"name"
];
the main array is:
[
"uid,nid,name",
"1,28807051203034,q",
"2,28807051203035,w",
"3,28807051203036,e",
"4,28807051203037,r",
"5,28807051203038,t",
"6,28807051203039,y",
"7,28807051203040,u",
"8,28807051203041,I",
"9,28807051203042,o",
"10,28807051203043,p",
""
]
i want it become
[
{uid:1,nid:28807051203034,name:q},
{uid:2,nid:28807051203035,name:w},
{uid:3,nid:28807051203036,name:e|,
{uid:4,nid:28807051203037,name:r},
{uid:5,nid:28807051203038,name:t},
{uid:6,nid:28807051203039,name:y},
{uid:7,nid:28807051203040,name:u},
{uid:8,nid:28807051203041,name:I},
{uid:9,nid:28807051203042,name:o},
{uid:10,nid:28807051203043,name:p},
]
i did it
theheadoptions=[
"uid",
"nid",
"name"
];
trs=[
"uid,nid,name",
"1,28807051203034,q",
"2,28807051203035,w",
"3,28807051203036,e",
"4,28807051203037,r",
"5,28807051203038,t",
"6,28807051203039,y",
"7,28807051203040,u",
"8,28807051203041,I",
"9,28807051203042,o",
"10,28807051203043,p",
];
for(i=1;i<trs.length-1;i++){
ndlevel=trs[i].split(splitter);
dddsss(ndlevel,theadoption);
}
function dddsss(arr,heads){
const obj = {};
arr.forEach(function(v,k){
index=k;
obj[heads[k]]=v;
});
console.log(obj);
}
Related
I have a JSON structure that has a lot of arrays.I want to check if there are duplicates.Not inside the same array but in different arrays.Also i want the other fields to stay as is.
Here is an example of my structure:
{
"Collection":[
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test1"
"test2"
"test3"
]
},
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test8"
"test2"
"test9"
]
}
]
}
And here is what I expect:
{
"Collection":[
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test1"
"test2"
"test3"
]
},
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test8"
"test9"
]
}
]
}
I don't know if this is relevant but this is a firestore collection.
O'k, I don't know Swift, but I can show you how to do it:
<?php
$json = <<<JSON
{
"Collection":[
{
"field1":[
"test1",
"test2",
"test3"
]
},
{
"field2":[
"test8",
"test2",
"test9"
]
}
]
}
JSON;
$entities = json_decode($json, true); // Decode JSON
$collection = $entities['Collection']; // Grab array of fields inside collection
$elements = []; // Initialize an empty array of unique elements
$result = [];
foreach ($collection as $index => $fieldObject) {
$fieldName = array_keys($fieldObject)[0]; // Get field name
// Get value from array of values of this field
foreach ($fieldObject[$fieldName] as $valueKey => $value) {
// Check if your value is not in array of unique elements
if (!in_array($value, $elements)) {
$elements[] = $value; // Add value if is not
// Add value to your new array
$result['Collection'][$index][$fieldName][] = $fieldObject[$fieldName][$valueKey];
}
}
}
$result = json_encode($result); // Encode it back to JSON
Here is the working example in sandbox: http://sandbox.onlinephpfunctions.com/code/a78cff49cc31c15e7e1373f7e1f66b7951f129e9
I have the following code where I design the format of a JSON-array dynamically from variables ==>
var Title = '"text_title"'
var Topic2 = '"text_topic2"'
var jsonData = [ ];
createWantedJSON("title", data[i].topic, jsonData)
function createWantedJSON(title, Topic2, arr) {
arr.push({
"Topic": title,
[Topic2] : {
"Topic3": [{
"Subitem1": "Text1",
"Subitem2": "Text2",
"Subitem3": "Text3"
}]
}
}
This goes fine for the key and value which are not nested. But I don't know how to make it dynamic for the content of Topic3. I tried making a variable with the following content =>
'"Subitem1": "Text1", "Subitem2": "Text2", "Subitem3": "Text3"' but then of course it sees the content as one string and not as elements of the nested Topic3 array. I guess this isn't that hard but I can't seem to fix it.
I have a js object array like this.
[
{
name:"Japanilainen ravintola Koto",
rating:3.9,
photo:[
{
height:2160,
html_attributions:[
"Hannes Junnila"
],
photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
width:2269
}
]
},
{
name:"Kin Sushi Helsinki",
rating:4.2,
photo:[
{
height:2988,
html_attributions:[
"Stephan Winter"
],
photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
width:5312
}
]
}
]
I get this array above by doing this for each.
response.results.forEach((entry)=>{
var restaurantName = {
"name" : entry.name,
"rating" : entry.rating,
"photo_reference" : entry.photos
}
arr.push(restaurantName);
});
res.send(arr);
And I send the array to my browser so I can see it.
What I am trying to do is to get photo_reference from the entry.photos
I tried entry.photos[0].photo_reference and many more ways and in all of them I am getting a cannot read properly, and now I am not sure how to get that information out.
I edited some of the variable names to make it easier to simulate here, but just map the objects in the photo arrays to their references, and you'll get an array of photo references.
const data = [
{
name:"Japanilainen ravintola Koto",
rating:3.9,
photo:[
{
height:2160,
html_attributions:[
'Hannes Junnila'
],
photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
width:2269
}
]
},
{
name:"Kin Sushi Helsinki",
rating:4.2,
photo:[
{
height:2988,
html_attributions:[
'Stephan Winter'
],
photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
width:5312
}
]
}
]
const arr = []
data.forEach((entry)=>{
var restaurantName = {
"name" : entry.name,
"rating" : entry.rating,
"photo_reference" : entry.photo.map(x => x.photo_reference)
}
arr.push(restaurantName);
});
console.log(arr);
entry.photoes is not defined in your response.results object array.. did you mean to access it as entry.photo (inside your foreach function) ?
I have an array of hashes that I would like to sort based on the :reference values. But some of the elements within the array do not have a :reference key and therefore cannot be sorted. Is there a way to ignore this field and just sort the hash elements that contain this key?
I've tried the following approach but I'm getting an argument error
ArgumentError: comparison of NilClass with String failed
sort_by at org/jruby/RubyEnumerable.java:503
arr1 = [{:reference=> "F123",
:name=> "test4"
},
{
:reference=> "ZA4",
:name=> "test3"
},
{
:reference=> "A43",
:name=> "test2"
},
{
:name=> "test1"
},
{
:name=> "homework1"
}]
arr1 = arr1.sort_by { |hash| hash[:reference] }
puts arr1
The correct output should look like this :
=> arr1= [
{:reference=>"A43", :name=>"test2"},
{:reference=>"F123", :name=>"test4"},
{:reference=>"ZA4", :name=>"test3"},
{:name=> "test1"},
{:name=> "homework1"}
]
You can only sort on values that can be compared, so if you've got values of different types it's best to convert them to the same type first. A simple work-around is to convert to string:
arr1.sort_by { |hash| hash[:reference].to_s }
You can also assign a default:
arr1.sort_by { |hash| hash[:reference] || '' }
Edit: If you want the nil values sorted last:
arr1.sort_by { |hash| [ hash[:reference] ? 0 : 1, hash[:reference].to_s ] }
If you don't mind temporary variables, you could split the array into two arrays, one containing hashes with :reference and the other those without:
with_ref, without_ref = arr1.partition { |h| h[:reference] }
Then sort the first one:
with_ref.sort_by! { |h| h[:reference] }
And finally concatenate both arrays:
arr1 = with_ref + without_ref
I have a document that looks like this:
{
"_id" : ObjectId("56fea43a571332cc97e06d9c"),
"sections" : [
{
"_id" : ObjectId("56fea43a571332cc97e06d9e"),
"registered" : [
"123",
"e3d65a4e-2552-4995-ac5a-3c5180258d87"
]
}
]
}
I'd like to remove the 'e3d65a4e-2552-4995-ac5a-3c5180258d87' in the registered array of only the specific section with the _id of '56fea43a571332cc97e06d9e'.
My current attempt is something like this, but it just returns the original document unmodified.
db.test.findOneAndUpdate(
{
$and: [
{'sections._id': ObjectId('56fea43a571332cc97e06d9e')},
{'sections.registered': 'e3d65a4e-2552-4995-ac5a-3c5180258d87'}
]
},
{
$pull: {
$and: [
{'sections._id': ObjectId('56fea43a571332cc97e06d9e')},
{'sections.registered': 'e3d65a4e-2552-4995-ac5a-3c5180258d87'}
]
}
})
I've looked in to $pull, but I can't seem to figure out how to make it work on an array of nested objects containing another array. The $pull examples all seem to deal with only one level of nesting. How do I remove the matching entry from the registered array of the item in the sections array with the _id that I supply?
You need to use the positional $ update operator to remove the element from your array. You need this is because "sections" is an array of sub-documents.
db.test.findOneAndUpdate(
{ "sections._id" : ObjectId("56fea43a571332cc97e06d9e") },
{ "$pull": { "sections.$.registered": "e3d65a4e-2552-4995-ac5a-3c5180258d87" } }
)