Create a Comma-Separated String from a Single Column of an Array of Objects

How do I create an Array of objects from a comma separated string field in MongoDB?

Simply set a new key newLanguages, instead of the existing key languages, which will stay as it was:

db.collection.updateMany({},
[
{
$set: {
newLanguages: {
$filter: {
input: {$split: ["$languages", ","]},
cond: {$gt: [{$strLenCP: "$$this"}, 0]}
}
}
}
},
{
$set: {
newLanguages: {
$map: {
input: "$newLanguages",
as: "item",
in: {name: "$$item", active: false}
}
}
}
}
])

See how it works on the playground example

Easy way to turn JavaScript array into comma-separated list?

The Array.prototype.join() method:

var arr = ["Zero", "One", "Two"];

document.write(arr.join(", "));

Extract a JSON array items to a single column separated with comma

The "shipmentItems" part of the input JSON is an array, so you need an AS JSON clause in the first explicit schema and an additional OPENJSON() call:

DECLARE @json nvarchar(max)
...

SELECT
j.[shipmentId], j.[orderNumber], j.[shipDate], j.[serviceCode],
a.[sku], a.[quantity]
FROM OPENJSON (@json, '$.shipments') WITH (
[shipmentId] bigint,
[orderNumber] nvarchar(60),
[shipDate] date,
[serviceCode] nvarchar(30),
[shipmentItems] nvarchar(max) AS JSON
) j
OUTER APPLY (
SELECT
STRING_AGG([sku], ',') WITHIN GROUP (ORDER BY [orderItemId]),
STRING_AGG([quantity], ',') WITHIN GROUP (ORDER BY [orderItemId])
FROM OPENJSON (j.shipmentItems) WITH (
[orderItemId] int '$.orderItemId',
[sku] nvarchar(max) '$.sku',
[quantity] int N'$.quantity'
)
) a ([sku], [quantity])

Result:

shipmentId orderNumber shipDate   serviceCode sku                            quantity
100003768 9648219086 2021-10-28 ups_ground SCPZRTS-TRAY-01,SCPXTSS-BAG-06 1,1

php, how to convert an array containing stdClass Object into comma separated list

Except for the comma separated list, what you have should work in theory, but the code doesn't at all match your array. You state $object not $array and the property is id not nid.

However, just extract the id column and implode. If you have PHP 7:

$list = implode(', ', array_column($output, 'id'));

For older versions:

$list = implode(', ', array_map(function($v) { return $v->id; }, $output));

How to push comma-separated string into a specific array field?

const result = "5f6ac,9bc1d".split(",").map(id => ({ _id: id }));

console.log(result);

comma separated string to object array to preserve datatype

Maybe you are looking for this -

        var result = Options[Name].ToString()
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.StartsWith("\"") ? x.Trim('\"') : (int.TryParse(x, out temp) ? (object)temp : (object)bool.Parse(x)))
.ToArray();

Update

For a generic way to accomplish what you want is to write logicto identify type of data in a separate function GetData -

    var result = Options[Name].ToString()
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => GetData(x))
.ToArray();

GetData(input) function -

    private object GetData(string input)
{
object data;
if (input.StartsWith("\""))
data = input.Trim('\"');
else if (bool.TryParse(input, out bool bTemp))
data = bTemp;
else if (int.TryParse(input, out int iTemp))
data = iTemp;
else if (double.TryParse(input, out double dTemp))
data = dTemp;
else
data = input;

return data;
}

You can extend this method to parse input string to any possible datatype.

Hope this answers your question.

Turn properties of object into a comma-separated list?

You can use this one-liner in modern browsers

Object.keys(person).map(function(k){return person[k]}).join(",");


Related Topics



Leave a reply



Submit