将多个可观察数组组合成新的对象数组(Combine multiple observable arrays into new object array)
我有3个可观察的数组,如下图。
persons = [ { "firstName":"john", "lastName":"public", "locationID":"1", "departmentID":"100" }, { "firstName":"sam", "lastName":"smith", "locationID":"2", "departmentID":"101" } ] departments = [{"departmentID": "100", "name": "development" }, {"departmentID": "101", "name": "sales" }] locations = [{"locationID": "1", "name": "chicago"}, {"locationID":"2", "name": "ny"}]
我试图将这3个结合到下面的结果中,
result = [ { "firstName":"john", "lastName":"public", "location":"development", "department":"sales" }, { "firstName":"sam", "lastName":"smith", "location":"ny", "department":"sales" } ]
为了获得所需的结果,我使用了可观察对象的map函数来给出新的对象数组。
this.store<Person>('persons') .map(function(person){ let p = new personDetail() p.firstName = person.firstName, p.lastName = person.lastName return p; })
PersonDetail
对象具有firstName
,lastName
,location
和department
属性。 如何查找可观察的部门并获取departmentID的匹配行以获取部门名称?我是rxjs图书馆的新手,请告诉我是否有更好的方法来达到预期的效果。
I have 3 observable arrays like below.
persons = [ { "firstName":"john", "lastName":"public", "locationID":"1", "departmentID":"100" }, { "firstName":"sam", "lastName":"smith", "locationID":"2", "departmentID":"101" } ] departments = [{"departmentID": "100", "name": "development" }, {"departmentID": "101", "name": "sales" }] locations = [{"locationID": "1", "name": "chicago"}, {"locationID":"2", "name": "ny"}]
I am trying to combine these 3 into below result ,
result = [ { "firstName":"john", "lastName":"public", "location":"development", "department":"sales" }, { "firstName":"sam", "lastName":"smith", "location":"ny", "department":"sales" } ]
To get the desired result, I have used map function on persons observable to give new object array.
this.store<Person>('persons') .map(function(person){ let p = new personDetail() p.firstName = person.firstName, p.lastName = person.lastName return p; })
PersonDetail
object hasfirstName
,lastName
,location
anddepartment
properties. How do I do a lookup into departments observable and get a matching row fordepartmentID
to get the department name ?I am new to rxjs library, please let me know if there is a better way to attain the desired result.
原文:https://stackoverflow.com/questions/41255718
最满意答案
http://qt-project.org/doc/qt-5/qstring.html#details
QString类提供Unicode字符串。
QString存储一串16位QChars,其中每个QChar对应一个Unicode 4.0字符。 (代码值高于65535的Unicode字符使用代理对存储,即两个连续的QChars。)
Unicode是一种国际标准,支持当今使用的大多数书写系统。 它是US-ASCII(ANSI X3.4-1986)和Latin-1(ISO 8859-1)的超集,所有US-ASCII / Latin-1字符都可以在相同的代码位置获得。
在幕后,QString使用隐式共享(copy-on-write)来减少内存使用量并避免不必要的数据复制。 这也有助于减少存储16位字符而不是8位字符的固有开销。
除了QString,Qt还提供了QByteArray类来存储原始字节和传统的8位'\ 0'终止的字符串。 对于大多数目的来说,QString是你想要使用的类。 它在整个Qt API中使用,Unicode支持确保您的应用程序易于翻译,如果您想在某个时候扩展您的应用程序市场。 QByteArray适合的两种主要情况是当你需要存储原始二进制数据,并且当内存保存是关键的时候(比如在嵌入式系统中)。
基本上QString很棒,几乎无后顾之忧。 你可以在任何地方使用它,无论你喜欢什么。 如果您经常遇到任何拖慢添加字符串的速度的问题,则有一种使用字符串生成器的特殊方法,但根据我的经验,在尝试使QString更好之前,还有很多其他地方需要改进。
并直接回答您的问题:
这样安全吗? c编译器是否确保返回值在内存中保留足够长的时间以供调用函数使用? (或者这会冒内存损坏的风险)。 如果是后者,返回QString的正确方法是什么? (结果var必须是静态的吗?结果必须是testclass的成员吗?)
在上述所有上述情况下,这是安全的。 只要任何函数具有QString的句柄,共享指针等就会将其保留在内存中。 一旦完全超出范围,它将自行清理。
QString包含常量是否重要? (什么id情况3分配结果到随机字符串)
不,没关系。
如果myfunc是一个我想从不同线程调用的静态方法? 我是否必须通过引用传递额外的Qstring以确保每个调用者获得自己的变量(并返回void)?
你应该用交叉线程保护包装它,比如
QMutexLocker
。更新:QMutexLocker示例
// In your constructor m_mutex = new QMutex(); // When accessing a shared element across threads { QMutexLocker locker(m_mutex); // Accessing a variable is now threadsafe! m_sharedDataString += "!"; }
希望有所帮助。
http://qt-project.org/doc/qt-5/qstring.html#details
The QString class provides a Unicode character string.
QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character. (Unicode characters with code values above 65535 are stored using surrogate pairs, i.e., two consecutive QChars.)
Unicode is an international standard that supports most of the writing systems in use today. It is a superset of US-ASCII (ANSI X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1 characters are available at the same code positions.
Behind the scenes, QString uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data. This also helps reduce the inherent overhead of storing 16-bit characters instead of 8-bit characters.
In addition to QString, Qt also provides the QByteArray class to store raw bytes and traditional 8-bit '\0'-terminated strings. For most purposes, QString is the class you want to use. It is used throughout the Qt API, and the Unicode support ensures that your applications will be easy to translate if you want to expand your application's market at some point. The two main cases where QByteArray is appropriate are when you need to store raw binary data, and when memory conservation is critical (like in embedded systems).
Basically QString is awesome and almost worry free. You can use it wherever, and however you like. If you are running into any sort of slow down from appending strings too often, there is a special way to use a string builder, but in my experience, there are plenty of other places to improve before trying to make QString better.
And to answer your questions directly:
Is this safe? Does the c compiler ensure the return value stays in memory long enough to be used by the calling function? (Or does this risk memory corruption). If the latter, what is the right way to return a QString? (Does the result var have to be static? Does result have to be a member var of testclass?)
In all of the above cases mentioned above, it is safe. Shared pointers and the like keep it in memory as long as any function has a handle to the QString. Once it goes completely out of scope, it will clean itself up.
Does it matter that QString contains constants? (What id case 3 assigned result to a random string)
No, it doesn't matter.
What if myfunc is a static method that I want to call from different threads? Would I have to pass in an extra Qstring by reference to ensure each caller gets their own variable (and return void)?
You should wrap it with cross thread protection, such as a
QMutexLocker
.UPDATE: QMutexLocker example
// In your constructor m_mutex = new QMutex(); // When accessing a shared element across threads { QMutexLocker locker(m_mutex); // Accessing a variable is now threadsafe! m_sharedDataString += "!"; }
Hope that helps.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如何在失败的情况下返回一个const QString引用?(How to return a const QString reference in case of failure?)[2023-12-09]
你不能。 要么不返回一个const引用,要么像这样使用一个局部静态变量: const QString& MyClass::getID(int index) const { static const QString emptyString; if (i < myArraySize && (myArray[i] != 0)) { return myArray[i]->id; // id is a QString } else { return empty ... -
由于发射和接收对象都在主线程中,因此Qt使用直接连接(该插槽在您发射时立即被调用)。 在这种情况下,你的本地字符串仍然在堆栈中。 但是,按值传递它总是更好,特别是在驻留在不同线程中的对象之间建立连接时。 QString使用隐式共享(aka copy-on-write),所以通过价值传递它并不需要太多花费。 Since emitting and receiving objects are both in the main thread, Qt uses direct connection (the slot ...
-
http://qt-project.org/doc/qt-5/qstring.html#details QString类提供Unicode字符串。 QString存储一串16位QChars,其中每个QChar对应一个Unicode 4.0字符。 (代码值高于65535的Unicode字符使用代理对存储,即两个连续的QChars。) Unicode是一种国际标准,支持当今使用的大多数书写系统。 它是US-ASCII(ANSI X3.4-1986)和Latin-1(ISO 8859-1)的超集,所有US-ASC ...
-
将不同类型的QList转换为QString的静态函数(Static function to convert QList of different types to QString)[2024-01-01]
笔记: 假设Product::at(int)正在返回一个Transaction ,给定了前面的问题。 我也假设OP在他写“静态”时表示“内置” 可以使用内置函数删除for 。 尽管如此,一些(很多?)会发现新语法不太容易理解。 QString Product::toString() { QStringList aggregate; std::transform(m_transactions.begin(), m_transactions.end(), ... -
你可以调用readAll()两次。 第二次,流位于文件结尾,因此readAll()没有任何可读的内容并返回空字符串。 在你的调试输出中打印fileText 。 You call readAll() twice. The second time, the stream is positioned at end-of-file, and so readAll() has nothing to read and returns an empty string. Print fileText in your deb ...
-
从55296(0xD800)到57343(0xdfff)的字符是代理字符 。 您可以将其视为后面角色的转义字符。 它们本身没有任何意义。 您可以通过运行来检查它: // QChar(0) was omitted so s and s1 start with QChar(1) for (int i = 1 ; i < 65536 ; i++) { qDebug() << i << QChar(i) << s[i-1] << s1[i-1] << (s[i-1] == s1[i-1]); } The ...
-
如果您的对象没有完全命名为“mylabel_”,我认为该功能不起作用。 如果要根据某些正则表达式查找标签,则需要使用findChildren函数的QRegExp版本 。 QList
labels = this->findChildren (QRegExp("mylabel_*")); 也就是说,你总是可以使用普通的c ++来按名称索引对象。 这样做的典型方法是使用地图。 它可能类似于Qt在幕后所做的事情。 如果您有一组相同类型的已知对象,这将非常有效。 #includ ... -
您当前版本中的文件calculator.pro显然是错误的。 它应该看起来更像这样: # Add more folders to ship with the application, here folder_01.source = qml/calculator folder_01.target = qml DEPLOYMENTFOLDERS = folder_01 QT += qml quick sensors svg xml # Additional import path used to resolve ...