首页 \ 问答 \ 将多个可观察数组组合成新的对象数组(Combine multiple observable arrays into new object array)

将多个可观察数组组合成新的对象数组(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对象具有firstNamelastNamelocationdepartment属性。 如何查找可观察的部门并获取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 has firstName, lastName, location and department properties. How do I do a lookup into departments observable and get a matching row for departmentID 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
更新时间:2021-12-03 13:12

最满意答案

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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)